Skip to content

TCP.poll is a receive with a deadline: a park waits on its fd and its clock, whichever fires first (#858) - #861

Merged
VictorTaelin merged 1 commit into
bendlang:mainfrom
aldeni:tcp-poll
Sep 19, 2026

Conversation

@aldeni

@aldeni aldeni commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

What changed

TCP.poll(sock, max, ms) — a receive with a deadline, shaped like UDP.poll (for #858):

def TCP.poll(sock: Socket, max: U32, ms: U32) ->
  IO(Socket & Result<&1, &1, U32 & String, Maybe<&1, String>>)

Done{None{}} when nothing arrives within ms (ms = 0: at once, as UDP.poll does), Done{Some{data}} when data does ("" is the peer's close, as TCP.recv answers it), Fail on an error. The handle threads back outside the Result, as every effect on a handle does.

The park carries the deadline. In the C loop, io_wait_for(w, fd, evts, time, more) parks an activation on an fd, on a deadline, or on both; io_wait_on becomes its fd-only case; io_wait polls the fd of every parked activation that has one (evts set) and wakes one when its fd is ready or its time has come; io_step parks a pure timer with no fd (evts 0) instead of a POLLIN on its ms value, which io_wait skipped anyway. The JS lane's waits already took fd and at together; io_park_on now passes at through.

Files: bend2/effs/tcp_poll.c and .js (the effect, udp_poll with recv and a timed park), bend2/base.bend (the def, after TCP.recv), bend2/comp.ts (the loop, +39/−12), tests/io/tcp_poll.bend. bend.ts is untouched.

Why

TCP.recv parks until the socket is readable, with no deadline, and a watchdog cannot close the socket from outside (Socket is a handle law, not Data — a design entry, not something this PR touches). So a Bend server could not drop an idle connection: a peer that connects and sends nothing holds its handler parked forever. A new effect rather than a third argument on TCP.recv, so no caller changes; poll rather than a new word, so it reads as the TCP side of UDP.poll.

How verified

tests/io/tcp_poll.bend: an accepted socket is polled empty at once, polled empty for 200 ms with IO.now on both sides (a 150 ms floor shows the loop really waited), then the peer sends and a 1 s poll answers it.

$ bun bend2/main.ts tests/io/tcp_poll.bend          # interpreted, 394 ms
none
timeout
waited
bend-poll
$ bun bend2/main.ts tests/io/tcp_poll.bend -o t && ./t   # native, 203 ms
none
timeout
waited
bend-poll

Regression, JS lane, every tests/io/*.bend compared to its #| lines: 81 pass; the 26 that fail fail identically on upstream/main (they expect stderr, an exit code, the network, or a C-only body — my harness only reads stdout). Native lane, the tests that touch the loop: sleep_timer, sleep_order, spawn_sleep, tcp_loopback, tcp_short_recv, tcp_send_slow_peer, tcp_spawn, udp_poll, udp_recv_park, udp_send_recv, chan_rendezvous, channel_fork_join, fiber_fork_join, and deadlock (with its stderr line and exit 1) all print their lines. bend 2.0.16, Linux x86_64, clang 22.

Notes

… clock, whichever fires first (bendlang#858)

TCP.recv parks until the socket is readable, with no deadline, and a
watchdog cannot close the socket from outside (Socket is a handle law,
not Data), so a Bend server could not drop an idle connection (bendlang#858).

TCP.poll(sock, max, ms) is UDP.poll's shape for TCP, with a wait: it
answers Done{None{}} when nothing arrives within ms (ms = 0: at once,
as UDP.poll does), Done{Some{data}} when data does ("" is the peer's
close, as TCP.recv answers it), and Fail on an error. The park carries
the deadline: io_wait_for(fd, evts, time, more) parks an activation on
an fd, on a deadline, or on both, io_wait_on is its fd-only case, and
io_wait polls the fd of every parked activation that has one (evts
set) and wakes one when its fd is ready or its time has come; io_step
parks a pure timer with no fd (evts 0) instead of a POLLIN on its ms,
which io_wait skipped anyway. The JS lane's waits already took fd and
at together; io_park_on now passes at through.

tests/io/tcp_poll.bend: an accepted socket is polled empty at once
(none), polled empty for 200 ms with IO.now on both sides (timeout,
waited), then the peer sends and a 1 s poll answers it (bend-poll).
Both lanes print the four lines (the interpreted run takes 394 ms, the
native 203 ms). tests/io on the JS lane: 81 pass, and the 26 that fail
fail the same on upstream/main (stderr, exit codes, network, C-only
bodies). Native, unchanged: sleep_timer, sleep_order, spawn_sleep,
tcp_loopback, tcp_short_recv, tcp_send_slow_peer, tcp_spawn, udp_poll,
udp_recv_park, udp_send_recv, chan_rendezvous, channel_fork_join,
fiber_fork_join and deadlock (with its stderr and exit 1) pass.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
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