Skip to content

Timeouts and cancellation for blocking effects: one mechanism instead of a deadline twin per effect #1034

Closed

Description

@aldeni

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:

  1. A combinator in Base, no new effects: race the action against IO.sleep(ms) with IO.fork and a Chan. 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.
  2. 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 more runs either way, and SO_ERROR is 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.recv waiter;
    • 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.

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

  1. aldeni commented on Sep 24, 2026

    @aldeni
    ContributorAuthor

    The EINTR bug from the last paragraph is fixed in #1036, independently of this design.

  2. VictorTaelin commented on Sep 27, 2026

    @VictorTaelin
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions