From 29094db0b500e175da142e1eb6366d38fa299d82 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 10 Nov 2023 14:34:28 +0100 Subject: [PATCH] Handle closed connections when sending to server Fixes #346. This is tricky, because it is codec exception in the node, which is obviously a bug so we don't necessarily want to accomodate with special handling logic for that on the server. In particular, it's hard to tell whether the server needs to send a response or not at this level. So it's better handled client-side. We need to notify the team about this. --- clients/TypeScript/packages/client/src/Connection.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/clients/TypeScript/packages/client/src/Connection.ts b/clients/TypeScript/packages/client/src/Connection.ts index fbdf78bcf7..62720c0058 100644 --- a/clients/TypeScript/packages/client/src/Connection.ts +++ b/clients/TypeScript/packages/client/src/Connection.ts @@ -168,9 +168,20 @@ export const send = async ( ): Promise => { const { socket } = context return new Promise((resolve, reject) => { + function onUnexpectedClose (code: CloseEvent['code'], reason: CloseEvent['reason']) { + reject(new JSONRPCError( + -32000, + 'Connection closed', + { code, reason } + )) + } + + socket.once('close', onUnexpectedClose) + send(socket) .then(resolve) .catch(error => reject(JSONRPCError.tryFrom(error) || error)) + .finally(() => socket.removeListener('close', onUnexpectedClose)) }) }