Skip to content

TCP.listen's backlog is 512, not 16: a burst of 10k connections is answered in full - #977

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
aldeni:tcp-listen-backlog
Sep 25, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
aldeni:tcp-listen-backlog

Conversation

@aldeni

@aldeni aldeni commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

What changes

bend2/effs/tcp_listen.c and bend2/effs/tcp_listen.js call
listen(fd, 512) instead of listen(fd, 16): the same number in both
lanes, the value Bun uses (Node uses 511). The kernel caps the backlog at
its own limit (net.core.somaxconn on Linux, 4096 by default;
kern.ipc.somaxconn on macOS, 128), so on every platform the accept queue
is 512 or as deep as the system allows.

(Revised after review: the first version used SOMAXCONN in C and the
platform's value in JS; one constant gives the same behaviour in both
lanes.)

A new test, tests/io/tcp_listen_backlog.bend, listens and then connects
32 times in a row without ever accepting: with a backlog of 16 the
eighteenth SYN is dropped and that connect stalls on retransmits, so a
regression shows as a hang rather than a wrong line; with 512 all 32
complete at once and the test prints its #| line.

Why

A backlog of 16 is the accept queue's depth. Once 16 handshakes are
waiting for TCP.accept, the kernel drops further SYNs and the clients
retransmit after 1 s, then 3 s -- the connection is not refused, it is
silently delayed, and under a burst many of them time out on the client's
side before the server gets to them.

Measured with demos/io_http_server built from this tree once with each
backlog, on one 4-core Linux machine: 10,000 connections opened at once,
each sending one GET /, 10 s window, one run each:

backlog answered timed out last answer
16 9,920 / 10,000 80 6.51 s
SOMAXCONN (4096, the first version) 10,000 / 10,000 0 3.39 s
512 10,000 / 10,000 0 2.37 s

ss -ltn shows the listener's Send-Q go from 16 to 512.

Test output

tests/io/tcp_listen_backlog.bend, all four lanes on this tree
(bun bend2/main.ts <file> [--check-only | -o x.js | -o x]):

All terms check.
32 connected, none accepted

Negative check: the same test run with the released bend 2.0.22, whose
runtime still has listen(fd, 16), hangs (killed by timeout 25, no
output) -- the eighteenth connect never completes.

The existing tests/io/tcp_listen_close, tcp_loopback, tcp_poll,
tcp_spawn, tcp_short_recv, tcp_send_slow_peer,
tcp_connect_black_hole and port_bound print their expected lines on
the check, interpreted, JS and C lanes of this tree. The cluster-backed
gates/ harness is not available in this environment; the test file
follows the tests/<ns>/[a-z0-9_]+.bend rule and is well under the token
ceiling.

Duplicate check

No open or closed issue or PR in bendlang/bend mentions backlog,
SOMAXCONN or the listen depth; WONTFIX.txt does not cover it.

Comment thread bend2/effs/tcp_listen.js Resolved
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026
…swered in full

A backlog of 16 is the accept queue's depth: once 16 handshakes wait for
TCP.accept the kernel drops further SYNs and the clients retransmit after
1 s, then 3 s -- not refused, silently delayed, and under a burst many
time out before the server reaches them. Measured with
demos/io_http_server built from this tree, 10,000 connections opened at
once each sending one GET, 10 s window: backlog 16 answers 9,920 with 80
timeouts, last answer at 6.51 s; 512 answers all 10,000, last at 2.37 s
(SOMAXCONN, 4096 here, also answered all 10,000, last at 3.39 s). ss -ltn
shows the listener's Send-Q go from 16 to 512.

512 in both lanes, the value Bun uses (Node uses 511): the same number
everywhere, and the kernel caps it at its own limit (net.core.somaxconn,
kern.ipc.somaxconn).

tests/io/tcp_listen_backlog.bend listens and connects 32 times without
accepting: all complete at once and it prints its line in the check,
interpreted, JS and C lanes; with the released 2.0.22 runtime (listen 16)
the eighteenth connect stalls on retransmits and the test hangs, so a
regression shows as a hang, never as a wrong line. tcp_listen_close,
tcp_loopback, tcp_poll, tcp_spawn, tcp_short_recv, tcp_send_slow_peer,
tcp_connect_black_hole and port_bound keep their output on all four lanes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aldeni

aldeni commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, that's simpler and better: one number in both lanes, the same behaviour everywhere up to the platform's cap. Done in 3188cdb: listen(fd, 512) in C and JS, with a comment that the kernel caps it (net.core.somaxconn / kern.ipc.somaxconn).

I re-measured the burst with demos/io_http_server from this tree (10,000 connections opened at once, each sending one GET /, 10 s window):

backlog answered timed out last answer
16 9,920 80 6.51 s
SOMAXCONN (4096) 10,000 0 3.39 s
512 10,000 0 2.37 s

So 512 loses nothing here. tcp_listen_backlog and the other TCP tests pass on the check, interpreted, JS and C lanes. I squashed the change into the PR's single commit and updated the description to match.

Comment thread bend2/effs/tcp_listen.c Outdated
close(fd);
return EINVAL;
}
// 512 in both lanes, as Bun and Node use; the kernel caps it at its own

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 512 in both lanes, as Bun and Node use; the kernel caps it at its own

these comments don't really add anything

Comment thread bend2/effs/tcp_listen.js Outdated
return io_fail(22);
}
if (sys.bind(fd, sys.ptr(at), 16) < 0 || sys.listen(fd, 16) < 0
// 512, as in tcp_listen.c: the kernel caps it at its own limit.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 512, as in tcp_listen.c: the kernel caps it at its own limit.

Asked in review. The commit message above keeps the reason: the same
number in both lanes, capped by the kernel at its own limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aldeni

aldeni commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Removed both in 7815ac2. The commit message still says why 512 (the same number in both lanes, capped by the kernel). tcp_listen_backlog and the other tcp_* tests and port_bound pass the check, interpreted, JS and C lanes.

@nicolas-abril nicolas-abril changed the title TCP.listen's backlog is SOMAXCONN, not 16: a burst of 10k connections is answered in full TCP.listen's backlog is 512, not 16: a burst of 10k connections is answered in full Sep 25, 2026

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both points addressed. Merged onto current main: tcp_listen_backlog passes on the check, interpreted, JS and C lanes, and with listen(fd, 16) restored it times out as intended; the other tcp_/udp_ tests and port_bound still pass.

@nicolas-abril
nicolas-abril merged commit 2ccb068 into bendlang:main Sep 25, 2026
phenomenon0 pushed a commit to phenomenon0/bend that referenced this pull request Sep 27, 2026
Canon fixed U02 (backlog 512, bendlang#977), U04 (Nat.min/max through the tail
cycle loop, bendlang#1061) and U22 (the boxed Bool reaching Bool.or, bendlang#1038); U08
and U21 answer in C now, U20 in interp and JS. Still REPRO: U01, U03/U03t,
U06, U10, U11, U14 spawn_sleep, U16-U19, U20 (C), U21 (interp, JS), and
F01-F11 -- F01, no wall clock: IO.now is still monotonic.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01S4szbK155RvUkyET5spFdw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants