From 29fcdcf367d9e64ca04b4af54c3837a7770cdc98 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Tue, 9 Jun 2026 13:51:03 -0400 Subject: [PATCH] tcp-listen: add SO_REUSEPORT so N workers can bind same port Lets bend (examples/cuda-fanout/gpu-worker.lsp) run multiple worker processes behind a single listening port. Each worker calls tcp-listen on the same port; the kernel distributes incoming connections across the bound sockets. Foxhop production use case: 2 bend workers per GPU host (3090 + 4090) to consume the .ready queue at 2x throughput without an external load balancer. --- c/builtins.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/c/builtins.c b/c/builtins.c index b736b7c..993d4bd 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -1224,6 +1224,12 @@ static Value bi_tcp_listen(Value *a, int n, Env *e) { if (fd < 0) return VAL_FALSE; int one = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + /* SO_REUSEPORT lets N processes bind() the same port. Kernel hashes + incoming connections across the listening sockets so each accept() + returns a fresh client to whichever process is ready. Used by bend + to run multiple workers behind one port without an external + distributor. */ + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY);