Skip to content

fix(serve-http): release legacy sessions whose client stream closed - #408

Merged
AVIDS2 merged 1 commit into
AVIDS2:mainfrom
jfsema:fix/http-session-disconnect-grace
Nov 27, 2026
Merged

AVIDS2 merged 1 commit into
AVIDS2:mainfrom
jfsema:fix/http-session-disconnect-grace

Conversation

@jfsema

@jfsema jfsema commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Branch: fix/http-session-disconnect-grace → main

Problem

MCP SDK clients can open a standalone GET /mcp SSE stream after
notifications/initialized and reconnect if that stream drops. A client that
exits without sending DELETE /mcp leaves its legacy session open until the
idle timeout, which defaults to 12 hours. The session counts against
MEMORIX_MAX_HTTP_SESSIONS during that time.
MCP session management

On a Windows workstation running several Claude Code windows, /health
reported 22–23 active sessions, but only 5 TCP connections were still
established, one per live client process.

Change

  • New http-session-stream-watch.ts tracks the GET streams of each session.
  • A session is closed, with the usual expired-session tombstone, when both of
    these hold:
    • its last SSE stream closed with status 200 and no new stream opened within
      MEMORIX_SESSION_DISCONNECT_GRACE_MS (default 120 000 ms; 0 disables);
    • the client sent no request after the stream closed.
  • The idle timeout still applies to:
    • sessions that never open a GET stream (raw HTTP clients, some rmcp setups);
    • clients that keep posting without a stream.
  • The idle GC and the disconnect path share one expireSession() helper.

Tests

  • tests/cli/http-session-stream-watch.test.ts has 10 unit tests with fake
    timers: reconnect within grace, activity after close, refused GET, concurrent
    streams, already-closed session, forget/dispose, disabled.
  • An end-to-end run against the built server on an isolated data dir, with a
    3 s grace window and the SDK StreamableHTTPClientTransport:
    • A: the client closes without DELETE → the next request gets 404 "expired"
      (the same script on 1.10.4 still gets 200);
    • B: a live idle client with its stream open is kept;
    • C: a session that never opens a stream is kept;
    • D: a client whose stream closed but keeps posting is kept.
  • In the archived CLI, project, and HTTP test run, 358 of 370 tests passed.
    The 12 failures were in the codegraph, context, identity,
    long-term, and purge CLI suites. A baseline run of those five suites
    on unmodified main produced 12 failures out of 36 tests, across four of
    the five suites. The reported errors included subprocess timeouts and an
    ENOENT.

Streamable HTTP clients built on the MCP SDK keep a standalone GET/SSE
stream open for their whole life but rarely send DELETE /mcp when they
exit, so their sessions lingered until the 12 h idle timeout.

A session is now closed when its last SSE stream has been closed for
MEMORIX_SESSION_DISCONNECT_GRACE_MS (default 2 min, 0 disables) and the
client sent no request since. Sessions that never open a GET stream and
clients that keep posting without a stream still follow the idle timeout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AVIDS2

AVIDS2 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Integrated in #411 with original contributor commits retained. Added regression coverage and fixes for timer overflow, queued/running POST protection, late close events after cleanup, and the ordering where a rejected concurrent GET finishes after the real SSE stream closes. Built production-host smoke passes for silent disconnect expiry, reconnection, open SSE, POST-only sessions, and POST activity after close. The same smoke now runs on all three desktop CI platforms. Included in 1.10.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants