Title: HttpServletStreamableServerTransportProvider orphans sessions on first transient SSE write failure (no grace period) · Issue #920 · modelcontextprotocol/java-sdk · GitHub
Open Graph Title: HttpServletStreamableServerTransportProvider orphans sessions on first transient SSE write failure (no grace period) · Issue #920 · modelcontextprotocol/java-sdk
X Title: HttpServletStreamableServerTransportProvider orphans sessions on first transient SSE write failure (no grace period) · Issue #920 · modelcontextprotocol/java-sdk
Description: Summary In HttpServletStreamableServerTransportProvider, a single failed SSE write immediately removes the session from the in-memory map, so the client's next POST gets Session not found even though the failure was transient (LB respons...
Open Graph Description: Summary In HttpServletStreamableServerTransportProvider, a single failed SSE write immediately removes the session from the in-memory map, so the client's next POST gets Session not found even thou...
X Description: Summary In HttpServletStreamableServerTransportProvider, a single failed SSE write immediately removes the session from the in-memory map, so the client's next POST gets Session not found even ...
Opengraph URL: https://github.com/modelcontextprotocol/java-sdk/issues/920
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"HttpServletStreamableServerTransportProvider orphans sessions on first transient SSE write failure (no grace period)","articleBody":"## Summary\n\nIn `HttpServletStreamableServerTransportProvider`, a single failed SSE write\nimmediately removes the session from the in-memory map, so the client's next\nPOST gets `Session not found` even though the failure was transient (LB\nresponse-timeout, NEG rebalance, pod eviction, laptop sleep, mobile network\nblip). The client is forced into a full `initialize` round-trip and loses\nany server-side session state.\n\nAdding a short, configurable **grace period** before `sessions.remove(...)`\n— during which a reconnect with the same session id reattaches — would solve\nthis without touching where sessions are stored or how resumability works.\n\nA secondary, unrelated observation about a duplicate `asyncContext.complete()`\ncall is noted at the bottom.\n\n## Relationship to existing work\n\nI want to flag upfront how this differs from related open issues/PRs, so it's\neasy to triage.\n\n- **#914** (`McpSessionStore` SPI) — changes *where* sessions live\n (pluggable store: Redis, JDBC, Hazelcast). Great for restart/cluster\n scenarios. Orthogonal to this issue: even with a Redis-backed store, one\n failed write to one client still triggers `sessions.remove(sessionId)`\n and orphans **that** client's session.\n- **#830** (`Last-Event-ID` resumability) — adds an event store and wires\n replay through `GET /mcp`. Complementary to this issue but requires the\n session to still exist when the client reconnects. With today's eager\n removal, replay is unreachable for the case described here.\n- **#107** (server-restart `Session not found`) — same symptom, different\n trigger (process restart vs in-process transient write failure). #914\n addresses #107; neither addresses the scenario below.\n- **#274 / #738 / #376 / #201** — persistence / clustering angles, all in\n the same family as #914.\n\nThe ask here is narrower than any of those: **don't drop the session on the\nfirst transient write failure**.\n\n## Version\n\n- `io.modelcontextprotocol.sdk:mcp-core` 1.1.0\n- JDK 25, Tomcat (Spring Boot 4), behind a GKE HTTP(S) LB\n\n## Reproduction\n\n1. Run any gateway built on `HttpServletStreamableServerTransportProvider`\n behind an L7 LB that caps per-response duration below the session lifetime\n (GKE `BackendConfig.timeoutSec` ≤ 60 s is a clean repro).\n2. Connect an MCP client (seen with `claude-code`, but any streamable-HTTP\n client triggers it).\n3. Wait for the LB to close the SSE stream (~60 s in our case).\n4. Observe — server log:\n\n ```\n KeepAliveScheduler: Failed to send keep-alive ping to session ...:\n Did not observe any item or terminal signal within 10000ms in 'source(MonoCreate)'\n ServletStreamableServerTransportProvider:\n Failed to send message to session ...: Client disconnected\n ```\n\n Client gets `Session not found` on its next POST.\n\n## Root cause\n\n`HttpServletStreamableMcpSessionTransport.sendMessage` (v1.1.0, lines 738–767)\nhard-codes session removal in the catch block:\n\n```java\n@Override\npublic Mono\u003cVoid\u003e sendMessage(McpSchema.JSONRPCMessage message, String messageId) {\n return Mono.fromRunnable(() -\u003e {\n ...\n try {\n ...\n String jsonText = jsonMapper.writeValueAsString(message);\n HttpServletStreamableServerTransportProvider.this.sendEvent(writer, MESSAGE_EVENT_TYPE, jsonText,\n messageId != null ? messageId : this.sessionId);\n ...\n }\n catch (Exception e) {\n logger.error(\"Failed to send message to session {}: {}\", this.sessionId, e.getMessage());\n HttpServletStreamableServerTransportProvider.this.sessions.remove(this.sessionId); // \u003c— here\n this.asyncContext.complete();\n }\n ...\n });\n}\n```\n\nNo grace, no policy hook, no listener. `sessions` is a `private final Map`\nand `HttpServletStreamableMcpSessionTransport` is a private inner class, so\ndownstream apps cannot override the behaviour without reflection.\n\n## Proposal\n\nTwo shapes, from least to most invasive:\n\n### 1. Configurable session-retention grace period (minimal, recommended)\n\nAdd `Builder.sessionReconnectGracePeriod(Duration)`, defaulting to\n`Duration.ZERO` (current behaviour, fully backward compatible). On write\nfailure:\n\n- mark the session as detached (new flag on the session transport);\n- schedule a removal task at `now + grace` on a shared scheduled executor;\n- on an incoming `GET /mcp` whose session id matches, clear the detach flag,\n cancel the scheduled removal, and let the existing `Last-Event-ID` replay\n path (SDK lines 327–349) or #830's event-store path run.\n\nWith `grace = Duration.ZERO`, behaviour is identical to today. With\n`grace \u003e 0`, transient blips no longer orphan clients. Composes naturally\nwith #914 (pluggable store) and #830 (replay).\n\n### 2. Session lifecycle listener (larger, general-purpose)\n\nAdd a `SessionLifecycleListener` interface on the builder with\n`onSessionDetached`, `onSessionReconnected`, `onSessionClosed`. Ship the\ncurrent eager-remove behaviour as the default listener; apps can register\na custom listener with whatever retention policy suits their deployment.\n\nI'd prefer option 1 — it solves the concrete problem without opening a\nbroader API-surface question.\n\n## Secondary observation — duplicate `asyncContext.complete()`\n\nSame file: the catch block in `sendMessage` (line 761) and `close()`\n(line ~811) both call `asyncContext.complete()`. When a write failure is\nfollowed by a close, the second call races with the servlet container's\nstate machine and produces:\n\n```\nFailed to complete async context ... Async state [COMPLETING]\n```\n\nCosmetic, but pollutes logs. A flag (e.g. `asyncContextCompleted` set on\nfirst call, checked before the second) would silence it.\n\n## Willing to contribute\n\nHappy to open a PR for either of the above if the maintainers would welcome\nthe contribution — just want to confirm the approach and that option 1 is\nthe direction you'd prefer before writing code.","author":{"url":"https://github.com/andersenleo","@type":"Person","name":"andersenleo"},"datePublished":"2026-04-15T07:51:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/920/java-sdk/issues/920"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0b22a97b-9825-f6e7-e446-8411e45fad9b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E2C:3AA36:1E8CA2C:291FAC3:6A598BC9 |
| html-safe-nonce | 55d1d71e3ba388131fc6f10210645d3f4c664d2d829726fcffcdead696d3e2e7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTJDOjNBQTM2OjFFOENBMkM6MjkxRkFDMzo2QTU5OEJDOSIsInZpc2l0b3JfaWQiOiIzNjU3ODIwNDkxNTM5Mzg1Mjg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3505eee2a9ba83b9d1ce4854090628acef90385b564c431340ad0698ade54e37 |
| hovercard-subject-tag | issue:4267191328 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/java-sdk/920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6e791b1147ae5c237af9b8f8d70aa157595565ab08c05dbbf28cb36d1d21c433/modelcontextprotocol/java-sdk/issues/920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6e791b1147ae5c237af9b8f8d70aa157595565ab08c05dbbf28cb36d1d21c433/modelcontextprotocol/java-sdk/issues/920 |
| og:image:alt | Summary In HttpServletStreamableServerTransportProvider, a single failed SSE write immediately removes the session from the in-memory map, so the client's next POST gets Session not found even thou... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andersenleo |
| hostname | github.com |
| expected-hostname | github.com |
| None | a540949572872b935b393b36db38922db390ae71c859537d741b8f3eb7e545b5 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/java-sdk git https://github.com/modelcontextprotocol/java-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 919609219 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/java-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 919609219 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/java-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624bb50a7497aa346bef8cc3743af408a9ea10ca |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width