Skip to content

gateway: leaving a session waits for model-gateway to detach the run - #37

Merged
jackeydou merged 1 commit into
mainfrom
gateway-detach-race
Oct 8, 2026
Merged

jackeydou merged 1 commit into
mainfrom
gateway-detach-race

Conversation

@jackeydou

Copy link
Copy Markdown
Owner

Summary

test_without_an_attached_stream_the_backend_is_never_called failed now and then under mise run check: the scripted backend got a call nobody had queued a reply for. It passes on its own. This came up on #36, but it is a race in code that PR does not touch.

Root cause. GatewaySession.__aexit__ closes its side of the Attach stream and cancels the stream at once. model-gateway detaches the run only when it reads that close, so for a moment after the session is left, the run's keys are still accepted. A call made in that window reaches the backend, then fails closed because no ack comes.

The fail-closed rule held throughout: no response went unrecorded. The cost was a wasted backend call, plus the flaky test.

Fix. Leaving the session waits, up to 10 s, for the gateway to end the stream. The gateway ends it only after it has detached the run. A stream that ends while the session is leaving is not an error. If the gateway takes longer than 10 s, a warning is logged and the session leaves anyway, as before.

Also in this PR: a BUGFIX.md entry, a CHANGELOG.md entry under Fixed, and a note in docs/services/model-gateway.md.

Test plan

  • New guard, test_leaving_a_session_returns_after_model_gateway_detached_the_run. It delays the gateway's detach by 0.2 s so the window is always open. It fails without the fix and passes with it.
  • mise run check
  • mise run test:docker: 149 passed.

Notes

The test:docker run also had one failure: test_takeover.py::test_a_live_worker_keeps_its_run_past_many_leases. It is another timing-sensitive test, with 0.3 s leases. It passed 6 of 6 runs on its own and does not go through the gateway session, so it is not part of this PR.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-10-08T00:29:35.159049Z 309bd52 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

GatewaySession.__aexit__ closed its side of the Attach stream and
cancelled it at once, while model-gateway detaches the run only when it
reads that close. For a moment after the session was left, the run's
keys were still accepted, and a call then reached the backend before
failing closed for want of an ack. That made
test_without_an_attached_stream_the_backend_is_never_called fail now
and then under load.

Leaving now waits, up to 10 s, for the gateway to end the stream, which
it does after detaching; a stream that ends while leaving is not an
error. The guard delays the gateway's detach so the window is always
open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jackeydou
jackeydou force-pushed the gateway-detach-race branch from 309bd52 to 0a7f867 Compare October 8, 2026 00:30
@jackeydou
jackeydou merged commit a9d2e1e into main Oct 8, 2026
1 check passed
@jackeydou
jackeydou deleted the gateway-detach-race branch October 8, 2026 00:34
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.

1 participant