fix(server): emit keep-alive newlines during /session/:id/message#25959
Open
nez wants to merge 1 commit intoanomalyco:devfrom
Open
fix(server): emit keep-alive newlines during /session/:id/message#25959nez wants to merge 1 commit intoanomalyco:devfrom
nez wants to merge 1 commit intoanomalyco:devfrom
Conversation
The POST /session/:sessionID/message handler awaits the entire SessionPrompt.prompt() before writing any response bytes. For long synchronous tool calls (large `kubectl get events -A`, multi-step `gh pr create` flows, etc.) the wait can exceed many minutes — during which no application bytes flow on the response stream and the TCP connection is held open with no traffic. HTTP clients with finite per-recv timeouts then ReadTimeout before any byte arrives. We observed downstream workflow runners hitting this on every long-running cluster-health agent run (60+ min) until they disabled their timeout entirely; that loses the ability to detect genuinely dead connections. Emit a `\n` every 30s while the prompt runs. The response remains application/json since JSON parsers ignore leading whitespace before the value. clearInterval on completion (and in the finally block) prevents stray writes from interleaving with the JSON body. Tested: `bun turbo typecheck --filter=opencode` passes.
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Bojun-Vvibe
added a commit
to Bojun-Vvibe/oss-contributions
that referenced
this pull request
May 6, 2026
- anomalyco/opencode#25959 keep-alive newlines on POST /session/:id/message [merge-after-nits] - anomalyco/opencode#25855 wide-text paste-summary order fix via Intl.Segmenter [merge-after-nits] - openai/codex#21290 extract codex-file-watcher crate from core [merge-after-nits] - openai/codex#21272 add 'compact' SessionStartSource with FIFO queue [merge-after-nits]
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.
Problem
The
POST /session/:sessionID/messagehandler awaitsSessionPrompt.prompt()to fully complete before writing any byte to the response stream:For long synchronous tool calls (large
kubectl get events -A, multi-stepgh pr createflows, anything that produces no streaming bus events between LLM calls), the wait can exceed many minutes during which no application bytes flow on the response stream. The TCP connection stays open with no traffic.HTTP clients with finite per-recv timeouts then
ReadTimeoutbefore any byte arrives. We hit this in production with a downstream workflow runner using httpx; the failure stack lands in_receive_response_headers:Failures were 100% reproducible on hourly cluster-health agent runs.
Fix
Emit a
\nkeep-alive every 30s on the response stream whileSessionPrompt.promptruns. The response stays validapplication/jsonbecause JSON parsers ignore leading whitespace before a value (verified viaJSON.parse("\n\n\n{...}")and Pythonjson.loads(b"\n\n\n{...}")).clearIntervalruns before the JSON body write so no keep-alive newline can interleave between bytes of the JSON value. Thefinallyblock covers the throw path.Scope
POST /session/:sessionID/messageis changed. The sibling/commandand/shellendpoints usec.json(msg)(non-streaming), which is a different code path with different semantics — left alone here to keep the diff minimal. If they hit the same problem in practice they can be migrated in a follow-up.response.json(), which callsJSON.parse(text)and tolerates leading whitespace. No SDK change needed.application/jsoncontent-type header is preserved; clients expecting a strict JSON byte stream still get a parseable body.Test plan
bun turbo typecheck --filter=opencodepasses