Skip to content

A TCP receive with a deadline, as UDP.poll: today a Bend server cannot drop an idle connection #858

Closed

Description

@aldeni

What Bend should do

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.
def TCP.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:

def conn_talk(s: Socket) -> IO(Unit):
  do IO<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.

bend 2.0.16, Linux x86_64.

Activity

  1. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    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.

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