Repository navigation
loom route: do not classify a stream from half a token (engine#3263) - #3264
bong-water-water-bong wants to merge 1 commit into
Conversation
…ollow-up)
`looks_sse()` decides from the first chunk's leading lines, and a TCP read can hand back `da` with `ta: {...}` still in
flight. On that read a real stream would be classified as an error body and returned to the client as JSON under the
upstream's status -- the same defect the classifier was added to remove, one layer down.
The first-byte wait now also waits for a decidable buffer: while the accumulated `first` carries no newline and is
shorter than 64 bytes, it keeps pulling chunks (inside the same 60 s bound, ending early when the upstream finishes). A
body that never gains a newline -- a small JSON error, plain text -- is decided at `done` or at 64 bytes, which is what
the JSON branch wants anyway.
The classifier's logic is exercised standalone, extracted verbatim from `serve.cpp` and compiled on its own: 8 arms --
real SSE event, `event:`-prefixed, `:`-comment-prefixed, a single `data:` line, plain JSON error, an HTML page, plain
text, and the split token prefix `da` which must NOT read as a stream. 0 failures. `g++ -std=c++26 -fsyntax-only
app/serve.cpp` clean. CI's loom_route row writes whole events, so the fragmented path is reasoned and unit-checked, not
integration-tested -- said plainly.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 7a6bce3: READY
The half of #3262 that the classifier alone could not carry, and it states the reason in the one line that matters: "The first byte is not the first TOKEN: a TCP read can hand us da or data with the rest still in flight, and classifying on that would call a real stream an error body." So the classifier's NeedMore is only useful if the caller actually waits — and this is the caller learning to.
The predicate reads correctly for the job: undecided while no newline and under a 64-byte floor (s.find('\n') == std::string::npos && s.size() < 64), the wait is the same condition variable the pump already signals, and the loop exits on pump->done as well — so an upstream that sends one long line and closes is decided as "complete without a newline" rather than waited on forever.
Two properties I would want stated, and one is a bound: the wait stays inside the existing bound ("still inside the same bound"), which is the only way this cannot become an unbounded stall on a child that sends da and then hangs — and the 64-byte floor means a peer that sends 64 bytes with no newline is classified rather than waited on further. Both are the difference between "wait for enough bytes" and "hang until the timeout saves us".
The pair to keep in mind at merge time: #3262 decides, #3264 waits. Landing either alone leaves the failure — the classifier without a waiter calls da a refusal, and the waiter without a classifier has nothing to wait for. If they land in separate builds, the intermediate state is the broken one, so the body should say so.
Not verified: the run (needs a built 1bit and loom-serve). I read the predicate, the wait predicate and the exit conditions, which decide whether this can stall.
|
Superseded by merged #3262 ( Measured on current #3262 removed
The size heuristic is the one substantive difference, and it costs latency on the path that matters most here: a loom-serve refusal is a small JSON object, so a 64-byte wait delays the 400/JSON branch on every refused request, where the prefix rule decides at the first Closing this keeps one carrier per finding, and #3263 can close with it: |
|
VERDICT at 7a6bce3: the fix is right and the loop it adds is unbounded — the 60 s bound covers the first wait only, so a loom-serve that sends The defect it fixes is real
The bound the comment claims is not thereconstexpr int kLoomFirstByteSecs = 60; // :1781
… pump->cv.wait_for(g, std::chrono::seconds(kLoomFirstByteSecs), …) → 504 // :1784 bounded
…
// "Wait (still inside the same bound) until the buffer carries a line break"
while (undecided(*first) && !pump->done) { // :1805 ← no deadline
if (!pump->cv.wait_for(g, std::chrono::milliseconds(100), [&] { … }))
continue; // ← and keeps re-arming
…
}The first wait is bounded and answers 504 with a message — that is #3255's review point, honoured. The new loop has no deadline: its only exit besides Fix: carry the deadline instead of the constant — const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(kLoomFirstByteSecs);
while (undecided(*first) && !pump->done && std::chrono::steady_clock::now() < deadline) {
if (!pump->cv.wait_until(g, deadline, [&] { return !pump->q.empty() || pump->done; }))
break;
…
}
if (undecided(*first) && !pump->done) { abort_upstream(); res.status = 504; /* same message, same word "decidable" */ return; }so a stall after a partial token is answered the same way a stall before the first byte is, and the comment becomes true. That also removes the 100 ms spin: Not checked: the live behaviour against a stalled loom-serve (that is what the 504 arm in #3255's tests covers for the first wait; there is no arm for "stalled after a partial token", which is why this is worth an arm as well as a deadline). |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 7a6bce3: CHANGES
#3262 merged ten minutes ago and already does this — it replaced looks_sse() with loom::loom_stream_shape() in app/loom_route.h, whose third answer NeedMore exists precisely for the half-token case: "da" is a prefix of data: and cannot be decided, so the caller loops on wait_until against a single deadline until the bytes decide or the upstream finishes. On current main that is app/serve.cpp:1776-1797. So the case this PR describes is fixed, and the loop it adds runs after that decision.
And the added loop is not merely redundant — it re-introduces the unbounded wait #3262 removed:
auto undecided = [](const std::string& s) { return s.find('\n') == std::string::npos && s.size() < 64; };
while (undecided(*first) && !pump->done) {
if (!pump->cv.wait_for(g, std::chrono::milliseconds(100), [&] { … })) continue;
…
}wait_for(100 ms) inside a while with no deadline is an unbounded total wait: the first loop has already returned (shape decided, or deadline hit → 504), and this second one then waits for a \n that a stalled peer may never send. The reachable case is pedestrian — an SSE reply whose first chunk is data: with the event's newline still in flight, and then the upstream stalls: main hands the provider a stream at the 60 s bound, this waits indefinitely (until the 3600 s read timeout or the client's stop()), with the client's connection open and no status line sent. That is the exact shape #3262's comment calls out ("worse than the 200 it used to get").
The merge itself is clean (merge-tree rc 0) and does not break the build — I checked, because the diff still mentions looks_sse as removed context and that would otherwise be a compile error — so this is a logic finding, not a mechanical one.
What I would do: close this as superseded by #3262, since the classifier and the bound it needs are both on main and the ten checks in tests/loom_route_test.cpp (including "da" → NeedMore and data: with no payload → Sse) are the arms for exactly this. If you would rather keep a change here, the one thing that is genuinely not covered by #3262's arms is a chunk boundary inside a payload line — a test at that boundary is worth having, and it needs no production code.
|
READY FOR REVIEW at That build is the first end-to-end compile of the change (a full build is not possible on this box tonight: disk 158 G at the time, below the 160 G floor), and it ran What to look at, since the diff is small and the risk is not in its size: the Honest limit, repeated from the issue because it is the reviewer's call and not mine to soften: the split read is reasoned, not observed. The classifier's arms were run standalone (8, 0 failures) and CI's fake writes whole events, so nothing here demonstrates a real |
|
VERDICT at 1. #3263's defect is gone on merged So the fragmented read now resolves to a stream — which is #3263's acceptance criterion — because #3262 ( 2. The textual merge is clean and the result is inert — which is worse than a conflict, because nothing fails. shape = loom::loom_stream_shape(*first);
if (shape != loom::StreamShape::NeedMore || pump->done) break; // <- decision made here
...
while (undecided(*first) && !pump->done) { … first->append(…); } // <- this PR's loop runs after it
...
if (shape != loom::StreamShape::Sse) { … } // <- reads the old shapeA merge would therefore close #3263 with no behavioural change and leave a loop that looks like the fix. The one thing it does add — appending late bytes to Recommendation: close this PR with its branch and close #3263 as fixed by |
|
Closing as superseded, on the owner's tonight row for #3263/#3264 ("land #3264 / #3269") — the measurement is in my 00:44Z comment above and it has not changed at this head ( Short form: Landing the issue correctly instead: #3263 is closed as fixed by |
|
VERDICT at 7a6bce3: READY — this is the caller-side half that #3262's pure classifier needed, and the reasoning is the same line of thought: the first byte is not the first token. auto undecided = [](const std::string& s) { return s.find('\n') == std::string::npos && s.size() < 64; };
while (undecided(*first) && !pump->done) { … }Waiting for a line break (or 64 bytes, or upstream done) is what makes Two notes, neither blocking: the |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Adjudication of the two verdicts at this head (7a6bce3f): the later CHANGES stands, and I verified both halves of it in the tree rather than on the prose. This matters tonight because #3264 is on c34's list — merging it would put an unbounded wait on the stick.
Main already has the classifier and the single bound:
app/loom_route.h:610-620 enum class StreamShape { Sse, NotSse, NeedMore };
// NeedMore = "the bytes are a prefix of a…" — the half-token case
app/serve.cpp:1776-1797 auto deadline = steady_clock::now() + seconds(kLoomFirstByteSecs);
if (!pump->cv.wait_until(g, deadline, …)) … ← one deadline for the whole decision
and this PR adds, after that decision has already been made:
+ auto undecided = [](const std::string& s) { … };
+ while (undecided(*first) && !pump->done) {
+ if (!pump->cv.wait_for(g, std::chrono::milliseconds(100), …)) continue;
wait_for(100 ms) in a while with no deadline is an unbounded total wait, and the reachable case is ordinary: an SSE reply whose first chunk is data: with the event's newline still in flight, then a stalled upstream — main hands the provider a stream at its 60 s bound, this waits until the 3600 s read timeout or the client hangs up, with no status line sent. That is the shape #3262's own comment calls "worse than the 200 it used to get", so the READY at 00:30:28 is superseded by the CHANGES at 00:30:49.
Recommendation stands: close as superseded by #3262 — the classifier, the bound and ten arms in tests/loom_route_test.cpp ("da" → NeedMore, data: with no payload → Sse) are all on main. The one thing #3262's arms do not cover is a chunk boundary inside a payload line; a test for that is worth having and needs no production code, so it is a follow-up, not a reason to hold c34.
|
CI status at this head, for the merge loop: green except |
Closes #3263. Follow-up to #3255 (
0592891), which is onmain.looks_sse()decides a stream from the first chunk's leading lines. A TCP read can returndawithta: {"choices": …}still in flight, and that read takes thereturn falsebranch — so the first bytes are treated as an error body,res.statusbecomes the upstream's 200, and the client gets JSON where it asked for SSE. That is the defect the classifier was added to remove, one layer down.What changed: the first-byte wait now also waits for a decidable buffer.
A body that never gains a newline (small JSON error, plain text) is decided at
doneor at 64 bytes — which is what the JSON branch wants anyway. Nothing else moves: same pump, same status handling, same SSE path.Verified, and its limit stated rather than implied. The classifier's logic extracted verbatim from
serve.cpp, compiled on its own, 8 arms, 0 failures:That last arm is the honest form of the claim: at that granularity the classifier must answer false, so the caller must not ask yet — which is the change. I have not seen the split on the wire; CI's
loom_routerow writes whole events and the device runs streamed whole events, so this is reasoned from the framing plus a unit check, not a reproduced failure.g++ -std=c++26 -fsyntax-only app/serve.cppclean,copyright.py --checkrc 0. One file, +14.