Skip to content

Demo bug: HTTP fetch truncates responses after two TCP reads #1309

Closed

Description

@cristianoc

Summary

The HTTP fetch demo closes the connection after two reads, even when part of the response remains unread. A response delivered in three chunks is truncated. The reproduction below runs the compiled demo with controlled receive results and shows the body ABC being printed as AB.

What you did

Read demos/io_http_fetch/main.bend. Its header comment states the design: "The response is read in two recvs, the second the tail or the EOF of Connection: close" (main.bend:3-4). The commit that added the demo says the same: "two sequential recvs since Connection: close bounds the body" (c04402dc). fetch_sent calls TCP.recv once, fetch_head calls it once more, and fetch_tail closes the socket.

What happened

Connection: close bounds the body: the server's close marks where the response ends. It does not bound the number of receives needed to reach that close. Each TCP.recv performs one recv() call and returns whatever it reads (tcp_recv.c:10, tcp_recv.js:16), so a response that arrives in three or more pieces is cut after the second, even when it is shorter than 8192 bytes.

A deterministic run of the emitted demo, with the TCP effects replaced by a schedule that returns an HTTP header plus A, then B, then C, prints:

close: receives=2, unread=["C"]
body="AB"

The complete body for that schedule is ABC.

Expected behavior

Receive until EOF (an empty read), with an explicit bound on the response size, then close. A test with the header separator and body split across at least three receives prints the full body.

bend --version

bun bend2/main.ts version reports bend 2.0.35; bun --version reports 1.4.2. Source baseline: 2bfc83fd6cc64cc08c6eaa225aacb69c86072788.

uname -sm

Darwin arm64

Reproduction

The probe checks the demo with Bend.book_valid, emits JS with Comp.js_book, and replaces only the effect registrations: connect and send succeed, and successive receives return the three chunks above, then EOF. It opens no network connection. Save as probe.ts in the repository root and run bun probe.ts:

const Bend = await import(process.cwd() + '/bend2/bend.ts');
const Comp = await import(process.cwd() + '/bend2/comp.ts');
import {writeFileSync} from 'node:fs';
const book=Bend.book_nil();
await Bend.book_load(book,'demos/io_http_fetch/main.bend','',new Map());
Bend.book_valid(book);
const injected = `
const chunks = ['HTTP/1.1 200 OK\\r\\nConnection: close\\r\\n\\r\\nA', 'B', 'C'];
let receives = 0;
$0eff['TCP.connect'] = {run: () => io_done(42)};
$0eff['TCP.send'] = {run: socket => io_tup(socket, io_done({$: 'Unit'}))};
$0eff['TCP.recv'] = {run: socket => { receives++; return io_tup(socket, io_done(chunks.shift() ?? '')); }};
$0eff['Socket.close'] = {run: () => { console.log('close: receives=' + receives + ', unread=' + JSON.stringify(chunks)); return {$:'Unit'}; }};
$0eff['IO.print'] = {run: text => { console.log('body=' + JSON.stringify(text)); return {$:'Unit'}; }};
`;
const js=Comp.js_book(book).replace('\ncli(process.argv.slice(1));', injected+'\ncli(process.argv.slice(1));');
const dir = (await import('node:fs')).mkdtempSync('/tmp/bend-http-probe-');
writeFileSync(dir+'/run.js',js);
const result = Bun.spawnSync([process.execPath,dir+'/run.js']);
process.stdout.write(result.stdout);
process.stderr.write(result.stderr);
process.exitCode=result.exitCode;

This exercises the demo's receive sequence in the emitted JS; it does not measure packet timing on a live connection.

Evidence

Scope

The request is for the demo's receive loop. HTTP stays out of Base per WONTFIX.txt:92, "No DNS, TLS, crypto or HTTP in Base (#846)".

Related reports

Closed #1119 concerns the http_body law and comment. This report concerns the TCP receive sequence.

Source baseline: 2bfc83fd6cc6.

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