Repository navigation
TCP.poll is a receive with a deadline: a park waits on its fd and its clock, whichever fires first (#858) - #861
Merged
Conversation
… 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
TCP.poll(sock, max, ms)— a receive with a deadline, shaped likeUDP.poll(for #858):Done{None{}}when nothing arrives withinms(ms = 0: at once, asUDP.polldoes),Done{Some{data}}when data does (""is the peer's close, asTCP.recvanswers it),Failon an error. The handle threads back outside theResult, 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_onbecomes its fd-only case;io_waitpolls the fd of every parked activation that has one (evtsset) and wakes one when its fd is ready or its time has come;io_stepparks a pure timer with no fd (evts0) instead of aPOLLINon itsmsvalue, whichio_waitskipped anyway. The JS lane's waits already tookfdandattogether;io_park_onnow passesatthrough.Files:
bend2/effs/tcp_poll.cand.js(the effect,udp_pollwithrecvand a timed park),bend2/base.bend(the def, afterTCP.recv),bend2/comp.ts(the loop, +39/−12),tests/io/tcp_poll.bend.bend.tsis untouched.Why
TCP.recvparks until the socket is readable, with no deadline, and a watchdog cannot close the socket from outside (Socketis a handle law, notData— 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 onTCP.recv, so no caller changes;pollrather than a new word, so it reads as the TCP side ofUDP.poll.How verified
tests/io/tcp_poll.bend: an accepted socket is polled empty at once, polled empty for 200 ms withIO.nowon both sides (a 150 ms floor shows the loop really waited), then the peer sends and a 1 s poll answers it.Regression, JS lane, every
tests/io/*.bendcompared to its#|lines: 81 pass; the 26 that fail fail identically onupstream/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, anddeadlock(with its stderr line and exit 1) all print their lines. bend 2.0.16, Linux x86_64, clang 22.Notes
CHANGELOG.mdis yours.--check); both branch fromupstream/main.