streamable-http server: stateful session workers ignore config.cancellation_token and stay alive until keep_alive after shutdown

Fermée
#1,426 1 commentaire 0 réactions 1 personne assignée Voir sur GitHub

Les mainteneurs répondent en général sous 3 jours

@jamadeo y travaille déjà.

Depuis le 3/11/2026.

  • #1427 par @xixi-box — ouverte

Évaluation

Cette issue n'a pas encore été évaluée.

Description

bug P2 ready for work T-transport

We run one StreamableHttpService per agent run: rmcp 3.7.0 with server and transport-streamable-http-server, LocalSessionManager::default(), and the default config plus with_cancellation_token(...). It sits behind axum::serve(...).with_graceful_shutdown(...). The client is a child process. When the run ends we kill it, cancel the token and wait for the server task. The client never sends DELETE.

The stateful session created by its initialize keeps running after that. In one trace the last tools/call response went out at 00:16:43.820 and our handler was dropped at 00:21:43.823. That is SessionConfig::DEFAULT_KEEP_ALIVE (300 s) after the last event the session worker handled. The worker task owns the handler, so whatever the handler holds lives that long too. For us that kept a per-run resource alive and held up teardown for five minutes.

Reading the code (the same paths are still on main):

  • StreamableHttpService::spawn_session_worker in tower.rs is a plain tokio::spawn. Its serve_server call creates its own CancellationToken. LocalSessionManager::create_session uses WorkerTransport::spawn(worker), which also starts from a fresh token. As far as I can tell, config.cancellation_token only reaches the SSE response streams and the stateless path.
  • The worker task keeps an Arc of the session manager, and the manager keeps the LocalSessionHandle and its event_tx. So dropping the service or router doesn't close the worker's event_rx either. Only DELETE, close_session or the idle timeout end the session.

The doc comment on StreamableHttpServerConfig::cancellation_token says cancelling it terminates all active sessions, so either the docs or the behaviour is off.

Is the token meant to end stateful sessions as well? If so, passing a child of config.cancellation_token into the worker looks like it would fix this, either through WorkerTransport::spawn_with_ct in create_session or by racing it in spawn_session_worker. A way to close every session from the service or the manager on shutdown would work for us too.

For now we keep the Arc<LocalSessionManager>, call close_session for every entry in sessions once the server task has finished, and keep run-scoped state out of the handler.

Possibly related: #1325 (in-flight calls are not cancelled on disconnect in the session-manager path) and #857.

Langage dominant
Rust
Étoiles
4.1k
Forks
694
Merge moyen
4 j 16 h
PR mergées (30 j)
43

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modelcontextprotocol/rust-sdk

Toutes les issues de modelcontextprotocol/rust-sdk

Issues similaires

Plus d'issues Rust

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.