Skip to content

loom route: looks_sse() can classify a fragmented first read as an error body, returning a real stream as JSON (engine#3254 follow-up) #3263

Closed

Description

@bong-water-water-bong

looks_sse() — the classifier #3255 added — decides from the first chunk's leading lines, and a TCP read can hand back half a token, so a real stream can still be returned to the client as a JSON error body.

Found in #3255's own review-fix commit while it was still in flight, and it is on main now (0592891), so this is the follow-up rather than a complaint about a draft.

app/serve.cpp's looks_sse() requires a line starting data: (or an event:/id:/: prefix line) in the first chunk:

size_t pos = s.find_first_not_of(" \t\r\n");
… if (line.rfind("data:", 0) == 0) return true; … return false;

A stream whose first read is da — with ta: {"choices": …} still in flight — takes the return false branch: first is treated as an error body, res.status becomes the upstream's (200), and the client is handed JSON where it asked for SSE. That is the same failure the classifier exists to remove (a client counting zero pieces on a body it cannot parse), one layer down, and TCP does not guarantee the first read ends at an SSE token boundary.

The measurement that makes it concrete, and its limit. The classifier's logic alone, extracted verbatim from serve.cpp and compiled on its own — 8 arms, 0 failures:

ok  a real SSE event is a stream                    ok  a plain-text body is not
ok  a plain JSON body is not                        ok  an event: line before data: is still a stream
ok  an HTML error page is not                       ok  a comment line before data: is still a stream
ok  a single data line without the blank separator  ok  a split token prefix ('da') is NOT a stream   <- the defect

The last arm is the honest statement of it: at that granularity the classifier must answer false, so the caller must not ask yet. I have not observed the split on the wire — CI's loom_route row writes whole events, and the device runs streamed whole events too — so this is reasoned from the framing plus a unit check, not a reproduced failure. Said plainly because "unlikely" is not the same as "cannot".

The fix (PR open against main): the first-byte wait also waits for a decidable buffer — while the accumulated first bytes carry no newline and are shorter than 64 bytes, keep 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. No change to the status handling, the pump, or the SSE path.

Activity

  1. bong-water-water-bong commented on Nov 22, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Fixed on main by #3262 (3f131a554, merged 00:29Z) — this issue can close.

    Your diagnosis is what the fix is built on: "at that granularity the classifier must answer false, so the caller must not ask yet". main no longer has that classifier:

    git show origin/main:app/serve.cpp | grep -c looks_sse         -> 0
    

    looks_sse is gone; the decision is a pure function in app/loom_route.h with three answers — Sse, NotSse, NeedMore — and NeedMore is precisely "the caller must not ask yet". serve.cpp accumulates under the same 60 s deadline while the answer is NeedMore (also stopping early on pump->done), then decides. Ten compiled arms in tests/loom_route_test.cpp cover it, mutation-checked:

    "da", "eve", "", "\r\n"            -> NeedMore      (mutation: guess instead -> 3 FAIL)
    data: …, data:, event:/id:/: before data: -> Sse     (mutation: don't skip markers -> 2 FAIL)
    {"error":…}, "{", <html>…</html>   -> NotSse
    

    Two notes for the record, since they are the parts your issue got exactly right:

    • The prefix rule beats a size threshold for this route: a loom-serve refusal is a small JSON object, and any "wait until N bytes" rule delays the 400/JSON branch on every refused request. { is a prefix of no marker, so the merged version decides it on the first byte while still refusing to guess on da.
    • Your caveat is preserved: the split was never observed on the wire (CI's loom_route row and the device runs write whole events), so the arms are the evidence and the wire case is reasoned — the arm "a half-read marker asks for more bytes" says exactly that, rather than pretending a reproduction exists.

    Separately, the other half of #3254's residual — the 60 s bound not stopping the upstream, so the 504 was delivered up to an hour late — went in the same PR (Client::stop() from abort_upstream()); app/loom_route.h's header comment credits the review. #3264 is the same fix against the pre-merge tree and I have asked for it to close unmerged.

  2. bong-water-water-bong commented on Nov 22, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Evidence for closing this as fixed, if you agree: it is fixed on main by #3262 (3f131a5, merged 00:24:48Z), measured on the exact shape this issue describes. Stub loom-serve sends "da", pauses, then "ta: {\"choices\":[]}\n\n"; the old classifier copied verbatim from 0592891 versus the one on main:

    OLD (0592891) on the first chunk "da"   -> looks_sse=false  (a real stream returned as JSON — this issue)
    MAIN (3f131a5) on the same sequence, after 0.12 s and 21 bytes -> Sse (the client gets its stream)
    

    The mechanism is the third answer this issue asked for in effect: loom_stream_shape returns NeedMore for "da" instead of guessing, and serve.cpp keeps accumulating under the same 60 s deadline. #3264 was opened for this issue and patches looks_sse, which no longer exists on main (0 occurrences) — so I have asked its author to close it rather than merge inert code; details and the merge-tree measurement are on #3264. I have deliberately not closed anything here: the PR says Closes #3263, and an issue already closed by another lane is the false-red shape that duplicate_fixes_check was fixed for tonight.

  3. bong-water-water-bong commented on Nov 22, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Closing as fixed on main by 3f131a5 (#3262, merged 00:24:48Z) — proven by measurement, as rule 3 asks.

    The exact shape this issue describes, driven through a stub loom-serve that sends "da", pauses, then "ta: {\"choices\":[]}\n\n", with the old classifier copied verbatim from 0592891 and the one on main:

    OLD (0592891) on the first chunk "da"   -> looks_sse=false   (a real stream returned as JSON — this issue)
    MAIN (3f131a5) on the same sequence, after 0.12 s and 21 bytes -> Sse   (the client gets its stream)
    

    looks_sse has 0 occurrences on main; loom_stream_shape gives the third answer (NeedMore) this case needed, and serve.cpp keeps accumulating under the same 60 s deadline until the line is complete. #3264 was opened for this issue and patched the deleted function — it is closed as superseded with the merge-tree measurement.

    The related bound #3262 left one branch short ("one complete non-SSE line, then silence" still waited unbounded) is tracked and being landed as #3268 → #3269, so nothing from this thread is left in a merged comment.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions