Skip to content

Commit dfa7435

Browse files
committed
refactor(mcp) Disable stdio server sink draining on close
1 parent 47b0916 commit dfa7435

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

mcp/src/main/java/org/springframework/ai/mcp/server/transport/StdioServerTransport.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -248,46 +248,50 @@ public Mono<Void> closeGracefully() {
248248
return Mono.fromRunnable(() -> {
249249
isClosing = true;
250250
logger.debug("Initiating graceful shutdown");
251-
}).then(Mono.defer(() -> {
252-
// First complete the sinks to stop processing
253-
inboundSink.tryEmitComplete();
254-
outboundSink.tryEmitComplete();
255-
return Mono.delay(Duration.ofMillis(200));
256-
})).then(Mono.fromRunnable(() -> {
257-
try {
258-
// Dispose schedulers first
259-
inboundScheduler.dispose();
260-
outboundScheduler.dispose();
261-
262-
// Wait for schedulers to terminate
263-
if (!inboundScheduler.isDisposed()) {
264-
inboundScheduler.disposeGracefully().block(Duration.ofSeconds(5));
265-
}
266-
if (!outboundScheduler.isDisposed()) {
267-
outboundScheduler.disposeGracefully().block(Duration.ofSeconds(5));
268-
}
269-
270-
// Only after schedulers are disposed, close the streams
251+
})
252+
// .then(Mono.defer(() -> {
253+
// // First complete the sinks to stop processing
254+
// inboundSink.tryEmitComplete();
255+
// outboundSink.tryEmitComplete();
256+
// return Mono.delay(Duration.ofMillis(100));
257+
// }))
258+
.then(Mono.fromRunnable(() -> {
271259
try {
272-
if (inputStream != System.in) {
273-
inputStream.close();
260+
// Dispose schedulers first
261+
inboundScheduler.dispose();
262+
outboundScheduler.dispose();
263+
264+
// Wait for schedulers to terminate
265+
if (!inboundScheduler.isDisposed()) {
266+
inboundScheduler.disposeGracefully().block(Duration.ofSeconds(5));
267+
}
268+
if (!outboundScheduler.isDisposed()) {
269+
outboundScheduler.disposeGracefully().block(Duration.ofSeconds(5));
270+
}
271+
272+
// Only after schedulers are disposed, close the streams
273+
try {
274+
if (inputStream != System.in) {
275+
inputStream.close();
276+
}
277+
if (outputStream != System.out) {
278+
outputStream.flush();
279+
outputStream.close();
280+
}
274281
}
275-
if (outputStream != System.out) {
276-
outputStream.flush();
277-
outputStream.close();
282+
catch (IOException e) {
283+
// Log but don't throw since we're shutting down
284+
logger.debug("Error closing streams during shutdown", e);
278285
}
286+
287+
logger.info("Graceful shutdown completed");
279288
}
280-
catch (IOException e) {
281-
// Log but don't throw since we're shutting down
282-
logger.debug("Error closing streams during shutdown", e);
289+
catch (Exception e) {
290+
logger.error("Error during graceful shutdown", e);
283291
}
284-
285-
logger.info("Graceful shutdown completed");
286-
}
287-
catch (Exception e) {
288-
logger.error("Error during graceful shutdown", e);
289-
}
290-
})).then().subscribeOn(Schedulers.boundedElastic());
292+
}))
293+
.then()
294+
.subscribeOn(Schedulers.boundedElastic());
291295
}
292296

293297
@Override

0 commit comments

Comments
 (0)