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
Gateway leaks half-closed sockets with unread upload bodies after 503 on PUT /v1/agent-device/upload/direct, exhausting kernel mbufs #1810
Simlock 0.8.0, gateway on macOS 26.7.3 arm64, Node 24.22.2.
What happens
When the gateway answers PUT /v1/agent-device/upload/direct/<id> with a 503, it ends its side of the connection but never reads or destroys the request body. One example of such a 503 is agent-device is not available on this simlock: the worker holding this device is not connected to the gateway, with reason WORKER_UNREACHABLE. Each such socket stays in FIN_WAIT_2, owned by the gateway process, with the client's unread upload still in its receive queue:
After about 3 days of uptime, the gateway held 23 of these sockets with 27.7 MiB unread, and the kernel mbuf pool was exhausted:
42996/43008 mbuf 2KB clusters in use
130570 KB allocated to network (95.7% in use)
1312654965 requests for memory denied
From then on, everything on the host that needs mbufs failed with ENOBUFS (No buffer space available), including Unix-socket child pipes and outbound TCP. For Simlock, that meant:
The worker's simctl list -j devicetypes runtimes output arrived truncated (87038 of 87880 bytes). iOS dropped out of the catalog and the gateway refused iOS leases with refused: ios is not available on this simlock:
A driver could not read its catalog error="DriverCrashError: Could not parse simctl device catalog: Unterminated string in JSON at position 87038 (line 1876 column 40)" platform=ios
CoreDevice's USB link to physical iOS devices went down. devicectl list devices reported USB-attached devices as localNetwork rather than wired, so Simlock marked every physical iOS device absent (not-present):
<udid> localNetwork disconnected paired
Queued lease requests ended after about 330 s with Lease request cancelled (INTERNAL).
Recovery
Killing the gateway process released the sockets and the pool. It was restarted by launchd; no reboot was needed:
1/37704 mbuf 2KB clusters in use
After that, devicectl showed the devices as wired, the catalog read succeeded again, and every physical iOS device came back ready/present.
Expected
When the gateway rejects an upload, it should drain the request body (up to a cap) or destroy the socket, so a rejected upload can't keep kernel buffers. Examples:
req.resume() before or after sending the error response, or
Connection: close plus req.socket.destroy() once the response is sent.
An idle timeout for half-closed sockets would also stop a stalled client from pinning buffers indefinitely.
Not reproduced: on macOS 26 (arm64), with Node 24.14.0 and 24.22.2, the gateway's listener closes a refused upload's socket about 0.5 s after the 503. No FIN_WAIT_2 socket stays open.
What closes it: @hono/node-server's drainIncoming (dist/index.mjs:808) runs when the response closes, then calls socket.destroySoon() after 500 ms. That sends the FIN and destroys the socket on finish.
What I tried, all through the real HttpGateway (src/http/server.ts) answering 503 to a PUT with a 1 TiB Content-Length while the client keeps sending:
case server socket client sees
body never read (routeByHeader path) closed @507-514 ms FIN, then EPIPE
first chunk read, then 503 (#relay) closed @509-516 ms ECONNRESET
agent-device-style http.request + closed ~500 ms after ECONNRESET / premature close
pipeline(file), over 192.168.1.26 the 503; netstat: only TIME_WAIT
About #1811: its regression test fails on main only because it mocks Socket.prototype.destroySoon to call end() and never destroy. Without that mock, the same test passes on main (the connection is cut). So the PR guards against the step that keeps the socket open, but nothing shows why that step fails. Its own body says "Not confirmed". Its 2 s timer would be a backstop, not a fix for a known cause.
What would let me reproduce it:
lsof -nP -iTCP:4700 -sTCP:FIN_WAIT_2 on the gateway, so we know the gateway process still holds the fd.
One rejected upload traced with NODE_DEBUG=net,http on the gateway, or a tcpdump -i any -w 1810.pcap port 4700 of it.
Where the 503 came from: no worker link at the call (routeByHeader), or the uplink dropping mid-relay (#relay). The gateway log at that time says which.
Anything between client and gateway (a proxy, tailscale serve, a load balancer), and the agent-device version on the client.
npm ls @hono/node-server in the gateway's install.
Activity
V3RON commentedon Nov 24, 2026
Not reproduced: on macOS 26 (arm64), with Node 24.14.0 and 24.22.2, the gateway's listener closes a refused upload's socket about 0.5 s after the 503. No FIN_WAIT_2 socket stays open.
What closes it:
@hono/node-server'sdrainIncoming(dist/index.mjs:808) runs when the response closes, then callssocket.destroySoon()after 500 ms. That sends the FIN and destroys the socket onfinish.What I tried, all through the real
HttpGateway(src/http/server.ts) answering 503 to aPUTwith a 1 TiBContent-Lengthwhile the client keeps sending:About #1811: its regression test fails on
mainonly because it mocksSocket.prototype.destroySoonto callend()and never destroy. Without that mock, the same test passes onmain(the connection is cut). So the PR guards against the step that keeps the socket open, but nothing shows why that step fails. Its own body says "Not confirmed". Its 2 s timer would be a backstop, not a fix for a known cause.What would let me reproduce it:
lsof -nP -iTCP:4700 -sTCP:FIN_WAIT_2on the gateway, so we know the gateway process still holds the fd.NODE_DEBUG=net,httpon the gateway, or atcpdump -i any -w 1810.pcap port 4700of it.routeByHeader), or the uplink dropping mid-relay (#relay). The gateway log at that time says which.tailscale serve, a load balancer), and the agent-device version on the client.npm ls @hono/node-serverin the gateway's install.Side finding: #1815.
Written by an agent.
fix(http): an early answer's linger removes its listeners, so a keep-…
docs(http): the unread-body rule names its constant, not a copied val…
fix(http): the unread-body linger runs on the Clock port, as every ti…
fix(http): cut a connection whose request body is still arriving 2 s …