mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4829] [Engine] an error after a stream has started ends with the API's own error frame, not HTTP text in the body or silence #5197
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5197
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4829
Author: @gdevenyi
Created: 10/31/2026
Status: 🔄 Open
Base:
main← Head:api/stream-error-frames📝 Commits (1)
6f201abStreams: an error after the 200 is the API's own last frame (chat: error chunk then [DONE]; messages: error event; responses: response.failed), not a second HTTP response in the body or a silent end📊 Changes
3 files changed (+203 additions, -0 deletions)
View changed files
📝
serve/server.py(+40 -0)📝
serve/test_parallel.py(+28 -0)➕
serve/test_stream_errors.py(+135 -0)📄 Description
Problem and result
When the engine keeps refusing a slot after a streamed request has got its 200, the server writes a second HTTP response into the SSE body. I reproduced it with the repo's fake engine process, nothing patched: the
BUSYALLprompt with 2 slots, the trigger oftest_bgen_refused_for_good_is_a_retryable_503_and_frees_the_slots.Before, the raw chat body ended like this (line endings shown as line breaks):
The messages and responses bodies ended with the same block. The openai and anthropic SDKs raised nothing, and the caller saw an empty turn.
Any other exception in a started stream ended the body with no last frame at all. I showed this only by injecting a RuntimeError into MockEngine.
After this change, both cases end with the frame the stream loops already use for other errors:
[DONE].errorevent,overloaded_errorfor GpuBusy andapi_errorfor the others.response.failed.The SDKs now see the failure. openai chat raises
APIError, anthropic raisesAPIStatusError, and the openai responses stream ends onresponse.failed.What the specs and this repo say:
openapi.yaml, chat streaming: "If a failure occurs after streaming has started, a data frame may contain a JSON object with anerrorfield instead of a completion chunk."_responsesdocstring in this repo: "once the stream has started they arrive as a response.failed event."Related discussion
No scope agreement was sought: this is a bug fix and no default changes.
answer_startedis false and re-raises otherwise, so a started stream still gets no frame.Approach
Only the stream loops of
_openai,_anthropicand_responseschange, plus three small helpers. The new except branches come after the existing ones. The_posthandlers, the non-streaming paths and errors before the stream are not touched, and a stream that raises nothing never reaches the new code.disconnected. Its state stayserror, because a server error did happen. The responses path keeps_responses_failed, which ignores that write failure.Validation
Revision:
c62d5e3aplus this one commit. Linux, Python 3.12.3, openai 3.42.0, anthropic 2.3.0. The unit tests, the differential and the fake-engine SDK checks use MockEngine and the repo's fake engine process, with no GPU. The real-engine runs are marked as such below.Passed:
New tests, with this change:
python -m unittest serve.test_stream_errors serve.test_parallel.ParallelService.test_bgen_refused_for_good_in_a_stream_is_each_apis_error_frameran 6 tests, OK.
The same command on
c62d5e3a, withserve/test_stream_errors.pyandserve/test_parallel.pycopied in:FAILED (failures=10, errors=1), counting subtests. The error is one test calling a helper that does not exist there. The two tests that pass there are guards: a client that left gets no frame, and a clean stream ends as before.Test set:
python -m unittest serve.test_server serve.test_responses serve.test_anthropic_base_url serve.test_chat_tool_results serve.test_context_overflow serve.test_images serve.test_stream_errors serve.test_parallel serve.test_quiet_cancel serve.test_request_accounting serve.test_fatal_recovery serve.test_queue_keepaliveran 506 tests in 188 s:
FAILED (failures=1, skipped=1). The one failure is listed below.Differential against
c62d5e3a, a local script that is not in this PR. It sends 13 requests through MockEngine: 10 normal ones across the three APIs, stream and non-stream, and 3 streamed ones refused with a 400 before the stream. With ids, timestamps and timings normalized and Date, Server and Content-Length left out, status, headers and bodies were byte-identical.SDK checks, local scripts that are not in this PR, on both trees:
BUSYALL, nothing patched. Before: no exception from any SDK. After:APIError,APIStatusErrorwithoverloaded_error, and eventsresponse.created,response.in_progress,response.failed.APIError,APIStatusErrorwithapi_error, and a stream ending onresponse.failed.Real engine, nothing patched. RTX 5080 16 GB, engine 0.1.55 from release v0.2.9 (the tarball's sha256 matches the release's
SHA256SUMS.txt), Qwen3.8-Flash-Next IQ3_S, context 65536. Before isc62d5e3a, after is6f201abf, on the same day.The trigger: the config sets
min_free_vram_mibto 15800 and another program holds 300 MiB of VRAM. Request A is running and request B is queued with its 200 already sent when the engine getskill -9. On B's turnensure_loadedfinds too little free VRAM and raises GpuBusy. Each API ran once with a raw client and once with its SDK, so 6 kills on each side:HTTP/1.0 503block[DONE], no HTTP blockAPIErrorwith the GPU messageHTTP/1.0 503blockerrorevent,overloaded_error, no HTTP blockAPIStatusError, typeoverloaded_errorHTTP/1.0 503blockresponse.failed, no HTTP blockresponse.in_progressresponse.failedwith codeserver_errorand the GPU messageThe Responses stream does not raise: the SDK hands
response.failedto the caller as an event.On both sides, all 6 times: A ended with its existing error frame, and a normal request right after reloaded the engine and returned 200 with finish reason
stop. With this change the server log has no traceback from these runs.Normal streams on both sides, before the kills and after them: the 3 SDKs and 3 raw clients, 12 streams per side. Chat ended on
stopand[DONE], Messages onmessage_stop, Responses onresponse.completed, and no raw body held an HTTP block.Real engine,
--parallel 2, after only: 488 streams with half or more dropped mid-body (details under Not run). Of the 182 streams read to the end, none held an HTTP block or a refusal message, and the 120 short chat ones ended on[DONE]. I did not check the last frame of the 62 long ones. 6 normal streams after each of the 2 runs ended properly, 12 in all.Failed:
serve.test_server.GpuChoice.test_hip_ordinal:AssertionError: '0,1' != '1,0'. It fails the same way onc62d5e3a, so this change does not cause it.Not run:
BUSYALLresult above comes from the fake engine only. On6f201abfwith--parallel 2, I sent 240 short streams with half dropped, then 248 long streams on all three APIs with 186 dropped mid-generation. The log shows 0 refused slots. It takes more than 6 refusals for one request to reach this path. These runs were not repeated onc62d5e3a, since nothing was refused to compare.Limits
--parallelof 2 or more and more than 6 refusals for one request. The real-engine one needs an engine crash with a request queued behind it, and too little free VRAM for the restart.ensure_loadedinsideService.runis confirmed by the real-engine runs. Unconfirmed static analysis: EngineStuck can come from there too.Retry-Aftercannot follow a 200, so that wait does not reach a streaming client. The slot refusal's message does say "try again in a moment".overloaded_errorinside a stream. Before the stream it is still a 503 with typeserver_error, as it was. This PR does not change that, so the two disagree.🤖 Generated with Claude Code
https://claude.ai/code/session_01W53iURzvDQMjnYemag8uvv
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.