Add timeout and fallback logging for closeGracefully() to prevent hanging shutdown
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 50/100
Piste de recherche
Lisez d’abord McpAsyncClient.closeGracefully(), puis examinez initializer.closeGracefully() et transport.closeGracefully() afin de comprendre leur comportement de fin. Vérifiez que la gestion des timeouts, l’avertissement et la fermeture forcée de secours, la gestion des erreurs et la fin finale rendent l’arrêt borné sans modifier le chemin graceful prévu.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Problem
When using McpAsyncClient.closeGracefully(), the client executes:
return this.initializer.closeGracefully()
.then(transport.closeGracefully());
Both initializer.closeGracefully() and transport.closeGracefully() return Mono<Void>.
However, if either of them hangs—for example:
- The underlying transport (HTTP/SSE/WebSocket) never completes
- The server doesn’t respond to shutdown
- A Reactor pipeline remains open (no
onComplete)
then the returned Mono never completes, causing the application to hang indefinitely during shutdown.
This results in JVMs or containers that never terminate, blocking CI/CD or production deployments.
Goal
Add a timeout and fallback mechanism to ensure that the client always terminates safely, even when the transport or initializer fails to complete.
Proposed Change
1. Wrap shutdown calls with timeout and fallback
Use Reactor’s timeout(Duration, fallbackMono) operator to guarantee a bounded shutdown duration.
public Mono<Void> closeGracefully() {
return Mono.defer(() -> {
long start = logger.isDebugEnabled() ? System.nanoTime() : 0L;
Duration timeout = Duration.ofSeconds(
Integer.getInteger("mcp.shutdown.timeout.seconds", 10));
Mono<Void> graceful = this.initializer.closeGracefully()
.then(transport.closeGracefully());
Mono<Void> fallback = Mono.fromRunnable(() -> {
logger.warn("closeGracefully() timed out after {} seconds; proceeding with best-effort shutdown.", timeout.getSeconds());
try {
this.transport.close(); // force-close if needed
} catch (Throwable t) {
logger.warn("Fallback forced close encountered error: {}", t.toString());
}
})
.then();
return graceful
.timeout(timeout, fallback)
.doOnError(e -> logger.warn("closeGracefully() failed: {}", e.toString()))
.onErrorResume(e -> Mono.empty()) // ensure app doesn't hang
.doFinally(sig -> {
if (logger.isDebugEnabled()) {
long durationMs = (System.nanoTime() - start) / 1_000_000;
logger.debug("closeGracefully() finished with signal={}, took {} ms", sig, durationMs);
}
});
});
}
Summary
Introduce a timeout and fallback mechanism for closeGracefully() to guarantee reliable termination, preventing hanging shutdowns when the transport or lifecycle initializer fails to complete.
- Langage dominant
- Java
- Étoiles
- 3.7k
- Forks
- 1.1k
- Merge moyen
- 1 j 15 h
- PR mergées (30 j)
- 9
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modelcontextprotocol/java-sdk
-
area/transport bug P2
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
modelcontextprotocol/java-sdk#1136 ·
-
area/client bug P2
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
modelcontextprotocol/java-sdk#1124 · 1 commentaire ·
-
ServerCapabilities.logging is added unconditionally, overriding the caller's explicit capabilities Ouvertebug P2 ready for work
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
modelcontextprotocol/java-sdk#1086 · 1 commentaire ·
-
enhancement good first issue P3
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
modelcontextprotocol/java-sdk#1067 ·
-
bug P2 ready for work
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
modelcontextprotocol/java-sdk#898 · 1 commentaire ·
Toutes les issues de modelcontextprotocol/java-sdk
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
elastic/gradle-plugins#157 ·
-
enhancement Tools
Difficulté 1/5 Moins d'une heure Accessibilité débutants 75/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
apache/rocketmq-dashboard#5008 ·
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
DETECT_PARAMETER_NAMES=false silently disables @ConstructorProperties-based Creator detection too Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
FasterXML/jackson-databind#6229 ·