Repository navigation
fix(demo): read fragmented HTTP responses until EOF - #1340
Conversation
|
Joint local verification of #1337–#1341 against main 565d7fd, including the constructor-model correction in #1338:
No physical GPU, macOS/Metal runtime, or mini-cluster gate was exercised. Separate PR source branches remain independent; the integration branch is local verification only. |
Lorenzobattistela
left a comment
There was a problem hiding this comment.
Thanks for this. The loop does fix #1309: over a real loopback socket, with the header separator and the body split across six writes, main prints an empty body and this branch prints ABC, on both the Bun JS and native C backends. Four things need fixing before merge, though.
1. A response that fits the limit is rejected.
The limit counts decoded characters, but each read is sized in bytes, and TCP.recv decodes every read on its own. Near the end of the budget, U32.max(U32.min(left, 8192), 1) asks for reads of 1 to 3 bytes, and those can cut a multibyte character in half.
To reproduce, run a local server that sends a 65536-character response: the headers plus ASCII up to 65535 characters, a short pause, then é (2 bytes), then close. Point the demo at it. With left == 1, recv(1) returns the first byte of é, which decodes to U+FFFD, so left drops to 0. The final 1-byte read then returns the second byte, which is also U+FFFD and not empty. Both backends exit 1 with HTTP response exceeds 65536 characters. If the last character is ASCII e instead, the same response is accepted and all 65498 body characters print.
The same per-read decoding also corrupts text away from the limit. If the server writes caf\xC3 and then \xA9, the demo prints caf��. This happens on main too, but reading until EOF means more reads and therefore more places to split. The fix is to bound the response in bytes and decode it once: read with TCP.recv_bytes, cap the total byte count, and decode UTF-8 at the end. That fixes both problems, and the "decoded characters, not wire bytes" caveat goes away. If you'd rather keep TCP.recv, the minimum change is to never request fewer than 4 bytes and to make the limit check unable to reject a response that is exactly at the limit. But the corruption above would remain.
2. A regression test is needed.
The issue asks for a test that splits the separator and the body across at least three receives. Nothing in gates/test.ts runs demos, so right now the fix is covered only by local smoke runs. Please add a test under tests/io/ that sets up a loopback listener, the way tcp_short_recv.bend / tcp_loopback.bend do. Have it send a response split across several writes, and end it with the #| lines it should print. A case at the limit, as in point 1, would also cover the limit logic.
limit, as in point 1, would also cover the limit logic.
3. Remove the duplicated branches.
The Fail arm (close, then IO.die) and the EOF arm (close, then fold the chunks) appear in both fetch_read and fetch_end, and the Fail arm a third time in fetch_sent. Move each into one helper. Also drop the cap: Bool parameter, since it is always U32.is_zero(remaining). A demo should be the shortest correct version.
4. Remove the CHANGELOG entry.
Release notes are written when a release is cut, so please leave CHANGELOG.md out of this PR.
How I reproduced it: the PR head is 5507220. I made a copy of the demo with TCP.connect("127.0.0.1", PORT) and ran it against a Bun Bun.listen server that writes chosen byte pieces with 400 ms pauses between them. I ran it both with bend file.bend (JS) and as a binary built with -o (native C).
| scenario | main | this PR (JS / C) |
|---|---|---|
separator and A/B/C over 6 writes |
`` (empty) | ABC / ABC |
| 65536 chars, ASCII at the end | not run | accepted / accepted |
65536 chars, é at the end, its 2 bytes arriving in separate reads |
not run | exit 1, "exceeds 65536" / same |
café, é split across 2 writes |
caf�� |
caf�� / caf�� |
The test imported demos/io_http_fetch/main.bend, which needed a special case in gates/_lib.ts pack() and still broke gates/safe.ts, whose tarball holds tests/ only. Copy the demo's reader into the test and drop the gate change. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Checked the merged byte reader in both Bun and native C with the committed real-loopback fixture: both print AéBC, 65516, é, empty:True and malformed:True. This exercises split UTF-8, the exact byte limit and EOF, in addition to the fragmented header/body case. The reader now decodes after collecting bytes and shares the failure/EOF helpers; the release note was removed. Thanks for identifying the character-vs-byte boundary error and adding permanent coverage. |
Fixes #1309.
The HTTP fetch demo called
TCP.recvtwice and closed, so aConnection: closeresponse that arrived in three or more pieces was cut short. It now reads until EOF (an empty receive), then closes and prints the body.TCP.recv_bytesand caps the response at 65536 bytes, headers included. Each receive asks for at most what is left of the budget (8192 at most). At exactly the limit, a one-byte receive must return EOF, otherwise the fetch fails withHTTP response exceeds 65536 bytes. ANatfuel of 65536 keeps the loop structurally recursive.cafécame out ascaf��). Base has no bytes-to-text conversion, so the demo carries a small decoder (utf8) that follows the same replacement policy as the TCP text codec, including an incomplete final character.List.foldl.Tests (
tests/io/):http_fetch_eof.bend: a real loopback listener sends the header separator, the body and a two-byteéacross separate writes; also a response of exactly 65536 bytes whose last character's bytes arrive separately, an empty response, and one with no header separator. The test carries its own copy of the demo's reader, since tests import only fromtests/.http_fetch_limit.bend: one byte over the limit fails with exit 1.Verified locally:
#|lines on the interpreted, JS and C lanes, each in about 1 s.ABC(main prints an empty body), and splitcaféandx€😀ñysent one byte per write decode correctly.utf8decoder matchesTextDecoderon 433 byte strings (overlong, surrogate, above U+10FFFF, truncated, stray continuation, plus random), on C, JS and the interpreter.gates/, Base or the compiler.🤖 Generated with Claude Code