Repository navigation
Description
What Bend should do
One way to bound how long any blocking effect may wait, and to release what the effect holds when the bound wins, instead of a deadline twin next to each blocking effect (TCP.poll beside TCP.recv; #1011 proposed TCP.connect_within beside TCP.connect and was closed in favour of this discussion).
What a program would write (the name is a placeholder):
# None{} if the action did not finish within ms; the effect it was parked on
# is cancelled and releases what it held (a connecting socket is closed).
r : Maybe<&1, Result<&1, &1, U32 & String, Socket>> <- IO.within(2000, TCP.connect(host, port))The two shapes from the review of #1011:
- A combinator in Base, no new effects: race the action against
IO.sleep(ms)withIO.forkand aChan. It bounds how long the caller waits; the losing action keeps running, keeps what it holds, and keeps the program from ending until it finishes. - A cancellable timeout in the runtime (
IO.within(ms, act)). Parks already take a deadline (io_wait_on(w, fd, evts, time, more)in C,io_park_on(fd, out, k, more, at)in JS). What is missing, as far as I can tell from the runtime:- a way for a wake to know that the deadline fired rather than the fd. Today
moreruns either way, andSO_ERRORis 0 on a socket that is still connecting, so TCP.connect_within connects with a deadline, as TCP.poll receives with one #1011 had to probe writability to tell the two apart; - a cleanup step per effect, so a cancelled park releases what it holds: close the descriptor of a connect in progress, free a receive buffer, remove a
Chan.recvwaiter; - what cancelling a sequence means. My guess: only the effect that is parked when the deadline fires is cancelled, effects that already completed stay done, and pure code between effects is not interrupted.
- a way for a wake to know that the deadline fired rather than the fd. Today
Questions I don't know the answer to: whether computations the action started with IO.spawn are cancelled with it; whether within nests (the inner deadline wins?); and whether the same mechanism should also serve an external stop, for example waking a TCP.accept so a server can stop accepting and drain on shutdown.
Why
The blocking effects in Base today, and what a call that never returns holds:
| effect | deadline today | a stuck call holds |
|---|---|---|
TCP.recv |
TCP.poll(sock, max, ms) |
the socket |
TCP.connect |
none: to an address that drops SYNs it waits for the kernel's own timeout (134 s measured here, Linux, tcp_syn_retries = 6) |
a descriptor |
TCP.send |
none: to a peer that stopped reading it parks until the peer reads | the socket |
TCP.accept |
none: a server cannot stop waiting for the next connection | the listener |
Chan.recv |
none | a waiter |
A network program needs a bound on each of them: a client that connects out under a total deadline, a server that drops a slow or non-reading peer, a process that stops accepting and drains on shutdown. With the combinator each call can be bounded for the caller, but a timed-out connect keeps its descriptor until the kernel gives up (about two minutes), so under load the descriptors pile up. That is why the cleanup step matters to me more than the waiting part.
One more thing I found while writing #1011, related to "a wake doesn't say why": in the JS lane, io_wait ignores the return value of select. When a signal interrupts it (EINTR), Linux leaves the fd sets as they were, so every parked fd is woken as if it were ready. Most effects just park again on EAGAIN, but tcp_connect.js reads SO_ERROR, which is 0 while the connect is in progress: with a signal handler installed by a user effect, TCP.connect("192.0.2.1", 80) plus a kill -INT answers Done, a socket that never connected. The native lane retries select on EINTR and stays pending. That one is a bug independent of this design, so I'll send a small PR for it separately.
I'd be glad to help implement whichever shape you prefer once the design is agreed.
bend 2.0.26, Linux x86_64.
Activity
aldeni commentedon Sep 24, 2026
The EINTR bug from the last paragraph is fixed in #1036, independently of this design.
The JS event loop waits again when a signal interrupts select, as the…
Bend 2.0.32: the issue triage, root fixes, one verdict everywhere
VictorTaelin commentedon Sep 27, 2026
Fixed in Bend 2.0.32 (573002f): IO.within races an action against a deadline. Cancelling the losing blocking effect is out of scope: see WONTFIX.