Handle disconnection events whilst executing requests

Summary:
If there's a device client disconnect during request execution, no response is ever given back to the client (flipperd).

This change effectively subscribes to client disconnect events and notifies flipperd of any disconnection during request processing.

Reviewed By: passy

Differential Revision: D37787986

fbshipit-source-id: 31737a50b83b0cbe4141ce814064aebef7e09bfc
This commit is contained in:
Lorenzo Blasa
2022-07-12 12:55:19 -07:00
committed by Facebook GitHub Bot
parent 01fc74d4f1
commit c01df31459
2 changed files with 24 additions and 1 deletions

View File

@@ -378,7 +378,23 @@ export class FlipperServerCompanion {
);
}
return pluginInstance.companionApi[api](...(params ?? []));
return new Promise(async (resolve, reject) => {
const closeHandle = () => {
reject(new Error('Client disconnected whilst executing request'));
};
client.once('close', closeHandle);
try {
const response = await pluginInstance.companionApi[api](
...(params ?? []),
);
resolve(response);
} catch (error) {
reject(error);
} finally {
client.off('close', closeHandle);
}
});
},
'companion-plugin-subscribe': async (clientId, pluginId, api) => {
const client = this.clients.get(clientId);