You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A TCP receive with a deadline, as UDP.poll: today a Bend server cannot drop an idle connection #858
A TCP receive that gives up after ms milliseconds, shaped like UDP.poll, which Base already has (a receive that answers None{} when nothing is there):
# as UDP.poll(sock, max): Done{None{}} when nothing arrived within ms;# ms = 0 answers at once, as UDP.poll does today.defTCP.poll(sock: Socket, max: U32, ms: U32) ->IO(Socket&Result<&1, &1, U32&String, Maybe<&1, String>>)
What a server writes with it — a keep-alive connection that reads the next request or drops the peer after ten seconds of silence:
defconn_talk(s: Socket) ->IO(Unit):
doIO<Unit>:
got : Socket&Result<&1, &1, U32&String, Maybe<&1, String>><-TCP.poll(s, 8192, 10000)
conn_reply(got) # Done{None{}}: the peer sent nothing in 10 s, close it
The C side is udp_poll.c with recv in place of recvfrom, and, for ms > 0, the parked wait of tcp_recv.c (io_wait_on(w, fd, POLLIN, ..)) given a deadline; the JS side is the same over io_park_on. I can send that as a PR if the shape is right.
Why
TCP.recv(sock, max) parks until the socket is readable, with no deadline, and the one way to end a parked receive from outside — a watchdog that closes the socket — cannot be written: Socket is a handle law of kind Type, so a second reference to it is refused (design, WONTFIX #825; this is not a request to change that):
def watchdog(+s: Socket, ms: U32) -> IO(Unit):
Error:
- expected : Data
- observed : Type
So a Bend server cannot drop an idle connection. A peer that connects and sends nothing (or sends a request header and never its last \r\n) holds its handler parked forever — the slowloris shape — and a proof-checked server that any client can pin down is not a server one can expose. Today the timeout has to live outside Bend, in a reverse proxy, and people are writing HTTP servers in Bend (#846).
The workaround that exists: a user effect can take a Base Socket handle (guide, Effects), so a program can ship its own tcp_poll.c and tcp_poll.js. It works, and it means every server re-implements in two languages a primitive the UDP side of Base already has.
Shipped in 2.0.17 as TCP.poll(sock, max, ms), from @aldeni's PR #861. A server can drop an idle connection now. The effect was reworked to park first and receive on the wake.
Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
Activity
TCP.poll is a receive with a deadline: a park waits on its fd and its…
TCP.poll parks first and recv's on the wake, the shape TCP.recv alrea…
VictorTaelin commentedon Sep 19, 2026
Shipped in 2.0.17 as
TCP.poll(sock, max, ms), from @aldeni's PR #861. A server can drop an idle connection now. The effect was reworked to park first and receive on the wake.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
TCP.poll is a receive with a deadline: a park waits on its fd and its…
TCP.poll parks first and recv's on the wake, the shape TCP.recv alrea…