fix: avoid AlreadySentError on duplicate SSE GET#20
Merged
dbernheisel merged 1 commit intodbernheisel:mainfrom Apr 28, 2026
Merged
Conversation
When a second `GET /mcp` arrives for an existing session, `maybe_track_session_stream/1` correctly responds with HTTP 409 and a JSON-RPC `-32000` error. However, the conn was already sent and halted at that point, so the surrounding `dispatch/2` clause then tried to call `put_resp_header` and `send_chunked` on the same conn, raising `Plug.Conn.AlreadySentError`. Branch on `conn.halted` after `maybe_track_session_stream/1` so the 409 response is returned as-is and SSE setup is skipped. Adds a regression test that performs two consecutive GETs on the same session id and asserts the second returns a clean 409 with the expected JSON-RPC error envelope.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19.
Summary
GET /mcparrives for a session that already has atracked SSE stream,
maybe_track_session_stream/1correctly respondswith HTTP 409 and a JSON-RPC
-32000error and halts the conn.dispatch(%Plug.Conn{method: \"GET\"}, opts)clausewas unconditionally calling
put_resp_header/send_chunkedonthe returned conn, which raised
Plug.Conn.AlreadySentError.conn.haltedaftermaybe_track_session_stream/1so the409 response is returned as-is and SSE setup is skipped.
Test plan
test/phantom/plug_test.exsopens twoconsecutive GETs with the same session id and asserts the
second returns
status == 409withcode: -32000and\"Only one SSE stream is allowed per session\".mainwithPlug.Conn.AlreadySentErrorraisedat
lib/phantom/plug.ex:285.mix test— 201 tests, 0 failures (1 pre-existingskip), Elixir 1.19.1 / OTP 28.1.
Notes for downstream users
While this lands, downstream apps can wrap
Phantom.Plugand rescuePlug.Conn.AlreadySentErrorto fall back to a 409 JSON-RPC errorthemselves. The wrapper is no longer necessary once this is released.