modelcontextprotocol / modelcontextprotocol/java-sdk

StdioClientTransport should support bounded child process termination

Aperta
#937 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement P2 ready for work
Lingua principale
Java
Stelle
3.7k
Fork
1.1k
Merge medio
1g 15h
PR unite (30g)
9

Descrizione

StdioClientTransport starts and manages a child MCP server process. During graceful shutdown, the SDK transport currently calls process.destroy() and waits for process.onExit().

That works when the child process responds to SIGTERM, but it can hang indefinitely if the child process ignores termination or becomes stuck. Downstream, we had to implement a custom McpClientTransport to ensure proxied MCP server processes are always cleaned up when the parent shuts down.

Current behavior

The current shutdown flow is effectively:

if (this.process != null) {
  this.process.destroy();
  return Mono.fromFuture(process.onExit());
}

There is no configurable timeout and no fallback to destroyForcibly().

Expected behavior

StdioClientTransport.closeGracefully() should support bounded process termination:

  1. Stop accepting new messages.
  2. Complete the transport sinks.
  3. Send graceful termination with process.destroy().
  4. Wait up to a configurable timeout.
  5. If the process is still alive, call process.destroyForcibly().
  6. Dispose the transport schedulers.

This guarantees that child MCP server processes do not survive parent shutdown indefinitely.

Proposed fix

Add a configurable process termination timeout to StdioClientTransport, with a sensible default.

Conceptually:

process.destroy();

return Mono.fromFuture(process.onExit())
  .timeout(processTerminationTimeout, Mono.defer(() -> {
    if (process.isAlive()) {
      process.destroyForcibly();
    }
    return Mono.fromFuture(process.onExit());
  }));

This could be exposed through the existing ServerParameters builder or a dedicated StdioClientTransport constructor/builder option.

Suggested regression test

Add a test with a child process that ignores SIGTERM or does not exit promptly.

The test should verify that:

  1. closeGracefully() completes within the configured timeout plus a small buffer.
  2. The child process is no longer alive after closeGracefully() completes.
  3. The transport schedulers and sinks are cleaned up.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da StdioClientTransport.closeGracefully() e verifica come ServerParameters configura il transport e come viene atteso process.onExit(). Aggiungi un timeout di terminazione configurabile, quindi esegui o aggiungi il test di regressione suggerito con un processo figlio che ignora SIGTERM. Il lavoro è completo quando l'arresto termina entro il margine del timeout, un processo figlio ancora attivo viene terminato forzatamente e scheduler e sink vengono ripuliti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.