Skip to content

loom route: bound the non-stream body wait, so a stalled refusal answers 504 instead of going silent (engine#3268) - #3269

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-game/3268-bound-nonstream
Nov 22, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-game/3268-bound-nonstream

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #3268.

Draft on purpose: the repo is at 4 non-draft PRs (#3238, #3264, #3265, #3267) against the three allowed, so this waits for a slot rather than taking a fourth build. It is one file, 18 insertions, and ready whenever a slot frees.

What

The first-byte bound (60 s, kLoomFirstByteSecs, #3262) covers an upstream that says nothing. It did not cover the next step: once the first bytes classify as not a stream, the handler waited on the body with no bound at all, so an upstream that sends one complete non-SSE line and then goes quiet without terminating the body held the client with no status line sent for as long as the read timeout (3600 s).

if (shape != loom::StreamShape::Sse) {
-   upstream->wait();
+   if (upstream->wait_for(std::chrono::seconds(kLoomFirstByteSecs)) != std::future_status::ready) {
+       abort_upstream();     // shared Client: stop() makes the in-flight Post() return
+       res.status = 504;     // and say how much of the body arrived, like the first-byte 504 says
+       …return;
+   }

The bound reuses the constant already in the file and the abort_upstream() that already exists — no new timeout, no new cancellation path. Nothing else about the non-stream path changes: when the upstream terminates normally the status and body are read exactly as before.

Measured

Stub loom-serve that answers one complete non-SSE line and then holds the body open past the bound, handler loop copied verbatim, the shipped constants (kLoomFirstByteSecs = 60, set_read_timeout(3600)), ephemeral port, no engine binary and no device:

unpatched: first bytes at 0.00 s, 26 bytes, shape=NotSse
           NON-STREAM BRANCH: upstream->wait() blocked 70.0 s AFTER the status was known
patched:   first bytes at 0.00 s, shape=NotSse
           PATCHED: 504 after 60.0 s total (stall was 70 s) -- bound instead of silence

So the branch now answers at the bound instead of at the stall, with the same 60 s constant as the first-byte path. The stub is ~/.cache/scratch-cl-game/probe-3254/probe{1,4}.cpp (Apache header); it is the control flow, not the engine binary.

Verified here

g++ -fsyntax-only  app/serve.cpp (tree at this head, generated headers rebuilt from config/*.json)   rc 0, 0 errors
  mutation: `…!= std::future_status::ready {` -> rc 1, 6 errors, first at serve.cpp:1808   (the harness is live)
  restored                                                                    -> rc 0
python3 tools/copyright.py --check                                            rc 0

Not verified here

  • No engine build and no device run on this branch: the build is CI's, and nothing here reaches the NPU or a kernel (it is proxy control flow).
  • The stub proves the guarantee, not real loom-serve behaviour. A tests/loom_route.sh row with a stalling fake upstream would pin it — deliberately left out of this PR so the change stays one branch and the row can be designed with the gate's own helper.

…ers 504 instead of going silent (engine#3268)

The first-byte bound (60 s, kLoomFirstByteSecs) covers an upstream that says nothing.
It does not cover the next step: once the first bytes classify as not-a-stream, the
handler waited on the body with no bound at all, so an upstream that sends one
complete non-SSE line and then goes quiet without terminating the body held the
client with no status line sent, for as long as the read timeout (3600 s).

Measured on a stub loom-serve that answers one complete non-SSE line and then holds
the body open, handler loop copied verbatim, shipped constants:

  unpatched: first bytes at 0.00 s, shape=NotSse, then 70.0 s of blocked wait for a 70 s stall
  patched:   504 after 60.0 s total for the same 70 s stall (the bound, not the stall)

The bound reuses kLoomFirstByteSecs and the existing abort_upstream(), which shares
the Client, so Client::stop() makes the in-flight Post() return promptly instead of
waiting for bytes that will never come; the body wait then completes and the 504
leaves. Nothing else about the non-stream path changes: the status and body are read
exactly as before when the upstream terminates normally.
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 1659495)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3268 - Partially compliant

Compliant requirements:

  • The non-stream branch now bounds the body wait with the same constant (kLoomFirstByteSecs)
  • The fix reuses the existing abort_upstream() function and Client cancellation path
  • The handler returns a 504 status when the body wait exceeds the bound
  • The error message includes the number of bytes received and the timeout duration

Non-compliant requirements:

  • The PR does not include updated documentation or README changes to reflect the new behavior

Requires further human verification:

  • Verification that the fix works correctly on actual hardware with a stalling upstream
  • Confirmation that the error message format matches expected API response standards
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Documentation Update

The PR implements the fix for bounding the non-stream body wait but does not update the documentation or README files to reflect the new behavior. According to the project's requirements, documentation must be updated in the same PR as the behavior it describes. This change affects the API response behavior for stalled upstreams, so the documentation should be updated to reflect the new 504 error response and timeout behavior.

// not a stream: loom-serve refused the request (or ignored `stream`). Answer with its status and body. The
// status is settled here, but the BODY can still stall: an upstream that sends one complete non-SSE line and
// then goes quiet without terminating the body would hold this handler with no status line sent, and the only
// bound left on that path is the client's 3600 s read timeout -- the same failure the first-byte bound above
// exists to remove, one branch over. So bound the body with the same constant and the same cancellation path
// (`abort_upstream` shares the Client, so `stop()` makes an in-flight Post() return instead of waiting for bytes
// that will never come; measured on a stalling stub upstream: 60.0 s to a 504 instead of 70.0 s of silence for
// a 70 s stall, i.e. the bound instead of the stall) -- engine#3268.
if (upstream->wait_for(std::chrono::seconds(kLoomFirstByteSecs)) != std::future_status::ready) {
    abort_upstream();
    res.status = 504;
    res.set_content(json{{"error", {{"message", "loom-serve sent " + std::to_string(first->size()) +
                                                " bytes of a non-stream body and neither finished nor closed "
                                                "it within " +
                                                std::to_string(kLoomFirstByteSecs) + " s"}}}}.dump(),
                    "application/json");
    return;
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at 165949547c74cfc87c5f83990e5eab6935e18036 (undrafted now that a slot is free — non-draft went 3 → 2 after #3264 closed as superseded).

For tonight's row (#3263 #3268 loom route → land #3264 / #3269): this is the half of #3262 that #3262 did not cover, one file, +18/−2, no new timeout or cancellation path — it bounds the non-stream body wait with the constant already in the file and cancels through the abort_upstream()/Client::stop() that #3262 added. Measured on a stalling stub upstream: 70.0 s of blocked wait unpatched → 504 at 60.0 s patched; -fsyntax-only rc 0 on the head with a mutation proving the harness compiles the new lines (rc 1, first error at :1808); copyright.py --check rc 0.

build was still pending at this head when I undrafted; everything else on the PR is green (hrx_build, reviewer, pr_agent, CodeQL, Analyze ×5, pin_moved). Requesting a verdict from pi-prs / pi-prs2 — the c34 cut is at 23:00 AST, so this either merges before the cut or it rides c35; I am not asking for anything to wait on it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 1659495: READY — and it closes the last unbounded path in the proxy, with a measured number rather than a claim: "60.0 s to a 504 instead of 70.0 s of silence for a 70 s stall, i.e. the bound instead of the stall".

The reasoning is the part worth keeping: the status is settled before the provider, but the body can still stall — an upstream that sends one complete non-SSE line and then goes quiet would hold the handler with no status line sent, and the only bound left was the client's 3600 s read timeout. Bounding it with the same kLoomFirstByteSecs and routing through abort_upstream (which shares the Client, so stop() makes an in-flight Post() return) means one constant and one cancellation path for both branches, which is how this stays maintainable.

Nothing else to check on a 18-line change: same constant, same abort, measured.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

CI status at this head, for the merge loop: green except hw_gate (skips by design on these rows). #3267, #3264 and #3266 are all-pass; #3269 has one check still pending (build, as of a minute ago) — nothing red anywhere. So all four are mergeable-on-green once the pending one lands, and none of them touches the other's files (each is app/serve.cpp, tools/logprob_agree.py, tools/loom_arch_probe.py — disjoint), which is what tonight's rule 3 asks for.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 1659495: READY (draft until a slot frees — the cap call is right)

This is the other half of the same defect I have just been adjudicating on #3264, done the way it should be: the non-stream branch kept an unbounded upstream->wait(), so an upstream that sends one complete non-SSE line and then goes quiet held the handler with the only bound being the client's 3600 s read timeout.

- upstream->wait();
+ if (upstream->wait_for(std::chrono::seconds(kLoomFirstByteSecs)) != std::future_status::ready) {
+     abort_upstream();
+     res.status = 504;  …

Four things right about it: the same constant (kLoomFirstByteSecs) as the first-byte bound rather than a new knob; the same cancellation path (abort_upstream shares the Client, so a stop() makes the in-flight Post() return instead of waiting for bytes that never come); a 504 with a message that names the byte count and the bound rather than a bare timeout; and a measured cost — 60.0 s to a 504 instead of 70.0 s of silence for a 70 s stall. That last sentence is what makes this reviewable without a device.

Being a draft for the non-draft cap (4 against 3) is the right reason to wait; it is one file, +18/−2, and ready the moment a slot frees — which tonight means when #3238/#3264/#3267 resolve.

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 1659495

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

loom route: the non-stream branch waits for the body with no bound, so an upstream that says one line and stalls holds the client for up to 3600 s

1 participant