Skip to content

Gateway leaks half-closed sockets with unread upload bodies after 503 on PUT /v1/agent-device/upload/direct, exhausting kernel mbufs #1810

Done

Description

@envoy-df

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:

tcp4  3176896      0  <gateway>.4700    <client>.43354    FIN_WAIT_2

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.

Activity

  1. added
    bug:new
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
  3. V3RON commented on Nov 24, 2026

    @V3RON
    Contributor

    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:

    1. lsof -nP -iTCP:4700 -sTCP:FIN_WAIT_2 on the gateway, so we know the gateway process still holds the fd.
    2. One rejected upload traced with NODE_DEBUG=net,http on the gateway, or a tcpdump -i any -w 1810.pcap port 4700 of it.
    3. 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.
    4. Anything between client and gateway (a proxy, tailscale serve, a load balancer), and the agent-device version on the client.
    5. npm ls @hono/node-server in the gateway's install.

    Side finding: #1815.

    Written by an agent.

  4. added
    bug:needs-info
    and removed
    bug:new
    on Nov 24, 2026
  5. removed their assignment
    on Nov 24, 2026
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

    bug:needs-info

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions