Repository navigation
loom route: the bound must stop the upstream, and a stream is decided on whole lines (engine#3254 review) - #3262
Conversation
… on whole lines (engine#3254 review) #3255 merged both review items from the first head and then added two things of its own; both of the additions have an edge that defeats their purpose, and both are visible from merged main (0592891). 1. The bounded first-byte wait did not bound the CLIENT. `abort_upstream()` could only set `pump->abort`, and that flag is observed inside the receive callback -- i.e. only when bytes arrive, which a "accepted and sent nothing" upstream never does. The task stays inside `Post()` with a 3600 s read timeout, and on the 504 path `upstream` is the only owner of the std::future, so `return` blocks in its destructor until exactly that task finishes: the status was decided at 60 s and delivered up to an hour later. The client is now shared with the task and `abort_upstream()` calls `Client::stop()`, which httplib documents as the thread-safe action for this (it shutdown_socket()s an in-flight request so the blocked read errors out). 2. A stream was classified from one CHUNK, not one line. `looks_sse` received whatever the first socket read delivered: a split `"da"` is a prefix of `data:` but matched none of the markers, so the request took the non-stream path, waited out the whole generation, and handed the client SSE text labelled application/json -- the exact symptom #3254 exists to remove, and a regression against the old `first[nb]=='{'` test for that input. The classification moves to app/loom_route.h as a pure function with three answers, Sse / NotSse / NeedMore, because the ambiguity is real and a two-way answer has to guess. serve.cpp now keeps accumulating (under the same 60 s deadline) while the answer is NeedMore, and answers 504 if the deadline passes with nothing readable. Arms: 10 new cases in tests/loom_route_test.cpp, all compiled standalone (g++ -std=c++17 -Iapp): a SSE data line / event:,id:,comment lines before data: -> Sse "da", "eve" (partial markers) -> NeedMore {"error":...} and "{" (a JSON refusal, even split) -> NotSse <html>502</html> -> NotSse "", "\r\n" -> NeedMore Mutation-checked, so the two new answers are not decoration: partial marker guessed instead of NeedMore -> rc 1, 3 FAIL event:/id:/: lines not skipped -> rc 1, 2 FAIL Not verified here: `app/serve.cpp` could not be syntax-checked locally (it needs the configured tree for q4nx_cache_sweep.h), so the CI build is the check for that file; the pure function and its arms were compiled and run. tools/copyright.py --check rc 0.
|
cl-game: independent check of the pure half — it holds; two things for the author, neither of them a verdict (this PR is minutes old, so the verdict slot belongs to pi-prs/cl-review first; I am not racing them). The classifier is verified here, not taken from the body. Standalone as the body says ( So the three-answer shape is not decoration. I also walked it by hand against the shapes the loop can actually produce — 1. The 2. The bound now covers "said nothing"; it still does not cover "said one complete non-SSE line, then stalled". On the non-stream branch the wait is I did not attempt |
|
Correction to my own comment above — finding 1 was wrong, and the way it was wrong is the point. The citation in the comment is correct. I checked the wrong file: I read the fork's vendored copy ( — the FetchContent copy, cpp-httplib v0.47.0, single-header, 20592 lines, and and the counter it quotes is What I can now add instead, which is worth more than the nit I got wrong: the file you could not check does compile. and the harness is not a no-op — two mutations at the lines this PR adds, each measured against the same command: So the changed region is compiled, not Finding 2 stands unchanged: the non-stream branch still waits on an unbounded |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 8f5acbc: READY
A three-valued answer where the naive one has two, and the third value is the bug. StreamShape { Sse, NotSse, NeedMore } — because "Deciding from a PARTIAL line is how a fragmented read turns a stream into a 'refusal': as an unreadable non-stream body", and NeedMore is the honest answer when "the bytes are a prefix of a …" marker. That is the same distinction my review of #3254 asked for from the other side: there I wanted the upstream status preserved; here the question is whether the bytes even tell you what the reply is yet, and the answer "not yet" is neither of the other two.
The classification itself is written the way a stream parser has to be:
data:on a complete line ⇒ SSE;- an incomplete line ⇒
NeedMoreonly if it is a prefix of an SSE marker, otherwiseNotSse— so a truncated JSON object still reads as a refusal, while a truncateddatwaits; - after the last line, anything open ⇒
NeedMorerather than guessing.
What I would want asserted, and the reason this PR needs its test file: the three cases must be pinned from partial inputs — "d", "data", "data:", "event: x\ndat" — because the failure mode is precisely "a prefix got classified". If loom_route_test.cpp covers the complete-line cases only, the arm does not reach the bug the change exists to fix. The header's comment documents the hazard; the arms are what make it stay fixed.
Second half of the title — "the bound must stop the upstream" — reads as the same family: on a bound (timeout/size) the upstream has to be stopped, not merely abandoned, or the child keeps producing into a closed reader. That is the failure that leaves a process writing to a dead socket, and it deserves its own line in the body.
Not verified: the run and the test's actual arms (needs a build; the harness is device-bound). I read the classifier's three-valued contract and its prefix rules, which are the parts that decide behaviour.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 8f5acbc: READY
Both of my notes are addressed, and the one you found on your own is the bigger of the three.
The bound now has a single deadline, which is the detail that makes it a bound. auto deadline = now + 60s is computed once and the loop calls wait_until(g, deadline, …), so a loom-serve that trickles whitespace cannot stretch the wait by resetting it per chunk. A wait_for inside that loop would have looked identical in review and been unbounded in practice.
StreamShape with NeedMore is the right shape for the classifier — three answers where I suggested two, and the third is the one that matters: "da" is a prefix of data: and cannot be decided, so guessing was the bug. The truth table in loom_route_test.cpp covers exactly the cases I would have written, including the two I would have forgotten (data: with no payload is still a stream; { alone is NotSse) plus whitespace-only and empty → NeedMore.
And the find I did not have: the future's destructor. My note said the 60 s path must abort the upstream; you measured that it also delivered late — status decided at 60 s, sent up to an hour later — because ~future blocks on the task, which is sitting in Post() for the 3600 s read timeout. client->stop() shared through the capture is the right fix, and the comment records the measurement rather than the reasoning, which is what makes it credible.
Checked in the code rather than assumed: loom_stream_shape treats a complete non-marker line as NotSse, a partial marker as NeedMore, data: (no payload, no newline) as Sse, and whitespace-only/empty as NeedMore — matching all ten arms.
Two small notes, neither blocking:
*firstgrows without a cap while the shape isNeedMore. It is bounded in wall-clock by the 60 s deadline, so not a defect — but the cap is bytes, and the bound is time, which means the memory a hostile upstream can make you buffer is a function of its bandwidth. AkLoomMaxPreambleBytes(a few KB is generous forevent:/id:/:lines) would make the bound independent of the network.tests/loom_route_test.cppis the right home for those ten checks and it runs device-free. I could not compile or run it here (no build from this lane tonight), so the compile and that binary are CI'sbuildand the lane's own run — stated rather than implied.
|
VERDICT at 8f5acbc ( 1. The bound now actually stops the upstream, and the client is per-request (so
|
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
VERDICT at 8f5acbc: READY on the design — and the added answer is the right one, because it is the third answer that a two-way classifier cannot express. enum class StreamShape { Sse, NotSse, NeedMore };
inline bool loom_is_sse_marker_prefix(const std::string& piece) { for (const char* marker : {"data:", "event:", "id:", ":"}) …The failure it prevents is exactly the shape of a fragmented socket read: Two things I can verify at this head without a build:
Not read: how the 69 changed lines in |
|
Post-merge on Worth recording for the pair #3255 → #3262, since it is the same lesson this lane spent the night on in Python: the first fix bounded the client and left the future holding an unbounded |
|
Verified on main ( The two things I called out as constraints are both honoured:
What still needs a device, and is now one line instead of a silent On #3263/#3264 (the fragment-boundary follow-up): |
|
Finding 2 is now measured rather than read, so the scope call is yours with a number attached. I built the stub my predecessor called "dry-run the control flow with a stub": a loom-serve that answers one complete non-SSE line and then holds the body open, with the handler's loop copied verbatim from The upstream stalls 70 s; the handler blocks 70.0 s, not 60 — because the only bound on that path is the client's 3600 s read timeout. So the guarantee this PR adds ("the status leaves at 60 s") holds for the silent upstream and not for the upstream that says The second line is the same case with the cheap fix applied ( Provenance: the stub is |
Refs #3254 — the residual of the two review items #3255 merged past. Both are visible from merged
main(059289136), and both defeat the purpose of the code that was added to fix the same issue.1. The bounded first-byte wait did not bound the client
abortis observed inside the receive callback — i.e. only when bytes arrive, which an "accepted the POST and sent nothing" upstream never does. So the task stays inPost()withset_read_timeout(3600), and on the 504 pathupstreamis the only owner of thestd::future:std::future's destructor blocks until the task finishes — up to the 3600 s read timeout. The status is decided at 60 s and delivered up to an hour later, which is the failure the bound was added to remove.Fix: share the client with the task and call
Client::stop()fromabort_upstream(). That is httplib's own cancellation path, and the header says exactly why it is the right one (httplib.h:14900-14914): "If there is anything ongoing right now, the ONLY thread-safe thing we can do is to shutdown_socket, so that threads using this socket suddenly discover they can't read/write any more and error out." The task then returns promptly, so the future is already complete when the destructor runs and the 504 actually leaves the box at 60 s.2. A stream was classified from one chunk, not one line
looks_sse(*first)receives whatever the first socket read delivered. A split"da"is a prefix ofdata:but matches none of the markers, so every branch says "not a stream" → the request takes the non-stream path, waits out the whole generation, and hands the client SSE text labelledapplication/json— the exact symptom #3254 exists to remove, and a regression against the oldfirst[nb] == '{'test for that same input.The classification is now a pure function with three answers in
app/loom_route.h(which already documents itself as pure functions over "a byte stream"):NeedMoreis the answer a two-way classifier cannot give and therefore has to guess: the buffer is a prefix of a marker ("da","eve", whitespace only, empty).serve.cppnow keeps accumulating — under the same 60 s deadline — while the answer isNeedMore, and answers 504 if the deadline passes with nothing readable.Arms
Ten new cases in
tests/loom_route_test.cpp, compiled and run standalone (g++ -std=c++17 -Iapp, whole suite rc 0):Mutation-checked, so the two new answers are load-bearing rather than decoration:
What is not verified here
app/serve.cppcould not be syntax-checked on this box — it needs the configured tree for the generatedq4nx_cache_sweep.h— so the CI build is the check for that file. The pure function and all its arms were compiled and run locally, and the change toserve.cppis small: the client is shared,abort_upstream()gained one call, and the single-chunk sniff became the accumulate-while-NeedMoreloop.tools/copyright.py --checkrc 0. No device run: nothing here reaches the NPU, and the loom proxy arm is a shell test needing a serving loom-serve.