Skip to content

Commit 40ddaa5

Browse files
committed
align with python and remove unused abost controller.
1 parent 674e79f commit 40ddaa5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/__fixtures__/serverThatHangs.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setTimeout } from 'node:timers';
1+
import { setInterval } from 'node:timers';
22
import process from 'node:process';
33
import { McpServer } from '../server/mcp.js';
44
import { StdioServerTransport } from '../server/stdio.js';
@@ -20,12 +20,22 @@ const server = new McpServer(
2020

2121
await server.connect(transport);
2222

23+
// Keep process alive even after stdin closes
24+
const keepAlive = setInterval(() => {}, 60_000);
25+
26+
// Prevent transport close from exiting
27+
transport.onclose = () => {
28+
// Intentionally ignore - we want to test the signal handling
29+
};
30+
2331
const doNotExitImmediately = async (signal: NodeJS.Signals) => {
2432
await server.sendLoggingMessage({
2533
level: 'debug',
2634
data: `received signal ${signal}`
2735
});
28-
setTimeout(() => process.exit(0), 30 * 1000);
36+
// Clear keepalive but delay exit to simulate slow shutdown
37+
clearInterval(keepAlive);
38+
setInterval(() => {}, 30_000);
2939
};
3040

3141
process.on('SIGINT', doNotExitImmediately);

src/client/stdio.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export function getDefaultEnvironment(): Record<string, string> {
9191
*/
9292
export class StdioClientTransport implements Transport {
9393
private _process?: ChildProcess;
94-
private _abortController: AbortController = new AbortController();
9594
private _readBuffer: ReadBuffer = new ReadBuffer();
9695
private _serverParams: StdioServerParameters;
9796
private _stderrStream: PassThrough | null = null;
@@ -126,17 +125,11 @@ export class StdioClientTransport implements Transport {
126125
},
127126
stdio: ['pipe', 'pipe', this._serverParams.stderr ?? 'inherit'],
128127
shell: false,
129-
signal: this._abortController.signal,
130128
windowsHide: process.platform === 'win32' && isElectron(),
131129
cwd: this._serverParams.cwd
132130
});
133131

134132
this._process.on('error', error => {
135-
if (error.name === 'AbortError') {
136-
// Expected when close() is called.
137-
return;
138-
}
139-
140133
reject(error);
141134
this.onerror?.(error);
142135
});
@@ -225,10 +218,18 @@ export class StdioClientTransport implements Transport {
225218
// ignore
226219
}
227220

228-
this._abortController.abort();
229-
230221
await Promise.race([closePromise, new Promise(resolve => setTimeout(resolve, 2_000).unref())]);
231222

223+
if (processToClose.exitCode === null) {
224+
try {
225+
processToClose.kill('SIGTERM');
226+
} catch {
227+
// ignore
228+
}
229+
230+
await Promise.race([closePromise, new Promise(resolve => setTimeout(resolve, 2_000).unref())]);
231+
}
232+
232233
if (processToClose.exitCode === null) {
233234
try {
234235
processToClose.kill('SIGKILL');

0 commit comments

Comments
 (0)