Repository navigation
Conversation
…IMEDOUT and closes its socket
TCP.connect waits for the kernel's own timeout: here (Linux,
tcp_syn_retries = 6) a native TCP.connect("192.0.2.1", 80) answered
"110 Connection timed out" after 134 s. TCP.poll gave a receive a
deadline (bendlang#858); this gives one to the connect, the one step a client
still could not bound.
TCP.connect_within(host, port, ms) is a new def beside TCP.connect,
which is unchanged. Both share tcp_connect.c/.js, each effect under its
own CID guard, as in file_read.c. The deadline is relative, as
TCP.poll's: io_tick() + ms * 1000000 to io_wait_on in C,
performance.now() + ms to io_park_on in JS; TCP.connect parks with none
(0, undefined), so its wake is the one it had. comp.ts does not change.
The park wakes for writability or for the clock and is not told which,
and SO_ERROR is 0 on a socket still connecting. So a wake of a park with
a deadline first checks, without waiting, that the socket is writable
(poll in C, select in JS): if not, the connect is still pending and
fails with ETIMEDOUT through io_fail, and the existing error path closes
the fd; if so, SO_ERROR says how it ended, as for TCP.connect. A
handshake that has finished by the wake answers how it finished, even
past the deadline, as a TCP.poll wake that finds data does.
tests/io/tcp_connect_within.bend passes the check, interpreted, JS and
native lanes, and four concurrent copies (three rounds, 12/12 on JS and
on C): a local listener connects and carries a send, the same port
after its listener closes answers "Connection refused", and 192.0.2.1
with ms = 200 fails before a 1500 ms timer (202 ms on the native lane).
With TCP.connect in its place the timer prints first and the program
does not end; without the writability check the deadline wake answers
Done. Scratch runs on each lane: ETIMEDOUT (110) at 201-202 ms for
ms = 200 and at once for ms = 0; 1000 timed-out connects under
ulimit -n 64, all ETIMEDOUT (no fd leak); ms = 0 to a local listener
connects 200/200, as Linux loopback finishes the handshake before
connect() returns.
tests/io, tests/base and tests/reg (236 tests) through the same four
lanes give identical results here and on 6a77e12. The branch merges
clean with bendlang#972, bendlang#977 and bendlang#978, pairwise and all four in every order
(one tree), and on that tree the eleven tcp_* tests and port_bound
pass on all four lanes.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the PR, and for how carefully it was tested. I'm going to close it, but the problem it points at is real, so here's what I think a better fix looks like. My concern is the shape: a deadline version next to a blocking effect. There are two ways I'd take it:
If you'd like to keep going, please open an issue about timeouts and cancellation in general so we can agree on a design first. Or send a new PR with approach 1, which would already cover most real uses. Either one is much easier for us to accept than per-effect variants. |
|
Thank you for closing it with the reasoning; that makes sense to me. One mechanism for every blocking effect is clearly better than a deadline twin for each of them. Two things I ran into while writing it, in case they help the design:
I'd be glad to open an issue on timeouts and cancellation with these notes, so we can agree on the design first. |
|
Opened #1034 with these notes, so the design can be discussed there. |
It answers as
TCP.connectdoes, with one more way to fail: if the handshakehas not finished
msmilliseconds after the call, the connect fails withETIMEDOUTand the socket it opened is closed.Why
TCP.connecthas no deadline. To an address that silently drops SYNs it waitsfor the kernel's own timeout: here (Linux, default
tcp_syn_retries = 6) anative build of
TCP.connect("192.0.2.1", 80)answered110 Connection timed outafter 134 s.TCP.poll(#861) gave a receive a deadline; connecting isthe one step left where a client cannot bound its time. A program with a total
deadline can refuse to start a connect once the deadline has passed, but
cannot bound one in flight, and nothing outside can end it: the program holds
no handle to a connect that has not answered.
Design
TCP.connectunchanged. The alternative is to addmstoTCP.connectitself; that breaks every caller, while the Base IO additionsin the changelog are new defs beside the old ones (
TCP.pollbesideTCP.recv,File.read_atbesideFile.read), and it is the shape TCP.listen_shared lets N processes share a port; TCP.listen keeps owning its own #972settled on after review (
TCP.listen_sharedbesideTCP.listen). The namereads as
read_atandsend_todo: a preposition naming the extraargument.
tcp_connect.cand.js, eacheffect under its own CID guard, as
file_read.cdoes.TCP.connectruns thesame code as before with no deadline on its park (
0toio_wait_on,undefinedtoio_park_on), so its wake is the one it had.comp.tsdoesnot change: both parks already take a deadline.
msis relative, as inTCP.poll: the deadline isio_tick() + ms * 1000000in C andperformance.now() + msin JS, taken when the effect runs.No special case for
ms = 0: the deadline is now and the poller answers atonce.
ETIMEDOUT, the error the kernel's own timeout gives(above), through
io_failon both lanes, so the text is libc'sstrerroras for every other failure; the fd is closed by the path that already closes
it on any error. No
Maybe: unlike aTCP.pollthat answersNone{}, atimed-out connect leaves no socket worth handing back.
on the clock, whichever fires first, and the wake is not told which. So a
wake of a park with a deadline first asks the socket, without waiting,
whether it is writable (
pollin C; in JSselect, the one readiness callio_sysbinds). Not writable: the wake came from the clock and the connectis still pending, so it fails with
ETIMEDOUT; a failedpoll/selectisread the same way. Writable:
SO_ERRORsays how the connect ended, exactlyas
TCP.connect's wake does. Without that check a deadline wake would readSO_ERROR = 0from a pending socket and answerDone. A handshake that hasfinished when the wake runs, even at or past the deadline, answers how it
finished, as a
TCP.pollwake that finds data answers it. On Linux this iswhat
ms = 0to a local listener shows: loopback has finished thehandshake by the time
connect()returnsEINPROGRESS, and 200 of 200such connects answered
Doneon the interpreted, JS and C lanes.Tests
tests/io/tcp_connect_within.bend, on the check, interpreted, JS and C lanes:TCP.listen, as theother TCP tests do) it connects and the socket carries a send and a recv;
Connection refused:the deadline rewrites no error;
192.0.2.1:80) withms = 200, beside a 1500 ms timer, itfails before the timer prints. The line names the outcome, not the error,
so a network that refuses the address at once prints the same lines.
Unlike
tcp_connect_black_hole, the test needs noIO.die: the connect endsat its deadline, so the program ends with the timer. The C lane here, each
line stamped by a wrapper as it arrived:
The JS lane printed the same lines at 29, 30, 231 and 1538 ms, and the
interpreted lane at 181, 182, 383 and 1686 ms (its start-up and check
included).
It fails on this machine, on all three run lanes, with either change undone:
TCP.connectin place of the 200 ms connect printstimerand then doesnot end (the harness's alarm kills it);
hole: connected.A scratch program printed
110 Connection timed outforms = 200(201-202ms) and for
ms = 0(0 ms) on each lane, and22 Invalid argumentfor a badhost or a port past 65535, as
TCP.connectanswers them. 1000 connects in arow to 192.0.2.1 with
ms = 0, underulimit -n 64, all answeredETIMEDOUTon the JS and C lanes: the timed-out socket is closed. Four concurrent copies
of the test, three rounds, passed 12/12 on the JS and on the C lane.
Regression:
tests/io,tests/baseandtests/reg(236 tests) through thesame four lanes give identical results on this branch and on 6a77e12: 172
pass every lane on both, and the other 64 fail a local runner simpler than the
gate the same way on both (expected checker errors, the
exit Nline the gateappends, mains that cannot be printed, tests with their own foreign twins).
Merge: clean with #972, #977 and #978, pairwise and all four together in every
order (one resulting tree). On that tree the eleven
tcp_*tests, the otherthree PRs' included, and
port_boundpass on all four lanes.Not tested
ETIMEDOUT= 60) and thezero-timeout
poll/selecton a connecting socket are unverified there; theloopback
ms = 0result above is Linux only, and the test does not rely onit.
to 192.0.2.1, as it does here. In a sandbox with no route the connect fails
at once, the test prints the same lines, and it does not discriminate.
forced; the rule for it is the one above.