From 8064338a73cf622ad112c34f4b3091446beaf2de Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Wed, 4 Sep 2024 06:04:56 +0800 Subject: [PATCH] fix(typescript-plugin): disconnect socket on error --- packages/typescript-plugin/lib/utils.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/typescript-plugin/lib/utils.ts b/packages/typescript-plugin/lib/utils.ts index f0e845f43e..1c4a0a5a2b 100644 --- a/packages/typescript-plugin/lib/utils.ts +++ b/packages/typescript-plugin/lib/utils.ts @@ -26,7 +26,7 @@ const pipes = new Map(); export const onSomePipeReadyCallbacks: (() => void)[] = []; -function watchNamedPipeReady(namedPipePath: string) { +function waitingForNamedPipeServerReady(namedPipePath: string) { const socket = net.connect(namedPipePath); const start = Date.now(); socket.on('connect', () => { @@ -49,6 +49,10 @@ function watchNamedPipeReady(namedPipePath: string) { pipes.delete(namedPipePath); socket.end(); }); + socket.on('timeout', () => { + pipes.delete(namedPipePath); + socket.end(); + }); } export function getNamedPipePath(projectKind: ts.server.ProjectKind.Configured | ts.server.ProjectKind.Inferred, key: number) { @@ -68,14 +72,14 @@ export function getReadyNamedPipePaths() { } else if (!pipes.has(configuredPipe)) { pipes.set(configuredPipe, 'unknown'); - watchNamedPipeReady(configuredPipe); + waitingForNamedPipeServerReady(configuredPipe); } if (pipes.get(inferredPipe) === 'ready') { inferredPipes.push(inferredPipe); } else if (!pipes.has(inferredPipe)) { pipes.set(inferredPipe, 'unknown'); - watchNamedPipeReady(inferredPipe); + waitingForNamedPipeServerReady(inferredPipe); } } return { @@ -104,11 +108,13 @@ export function connect(namedPipePath: string, timeout?: number) { pipes.delete(namedPipePath); cleanup(); resolve('error'); + socket.end(); }; const onTimeout = () => { cleanup(); resolve('timeout'); - } + socket.end(); + }; const cleanup = () => { socket.off('connect', onConnect); socket.off('error', onError);