Skip to content

Commit 7e6a867

Browse files
fix: ensure stdio closes server
1 parent 9757ace commit 7e6a867

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/client/stdio.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,30 @@ export class StdioClientTransport implements Transport {
210210
}
211211

212212
async close(): Promise<void> {
213-
this._abortController.abort();
214-
this._process = undefined;
213+
if (this._process) {
214+
const processToClose = this._process;
215+
this._process = undefined;
216+
217+
const closePromise = new Promise<void>(resolve => {
218+
processToClose.once('close', () => {
219+
resolve();
220+
});
221+
});
222+
223+
this._abortController.abort();
224+
225+
// waits the underlying process to exit cleanly otherwise after 1s kills it
226+
await Promise.race([closePromise, new Promise(resolve => setTimeout(resolve, 1_000).unref())]);
227+
228+
if (processToClose.exitCode === null) {
229+
try {
230+
processToClose.kill('SIGKILL');
231+
} catch {
232+
// we did our best
233+
}
234+
}
235+
}
236+
215237
this._readBuffer.clear();
216238
}
217239

0 commit comments

Comments
 (0)