Skip to content

forward: reuse connections to the target (keep-alive); a long run exhausts ephemeral ports on Windows #556

Closed

Description

@7schmiede

Problem

forward on a static service (0.49.1, #551) opens a new TCP connection to the target for every request and does not reuse it. Under the load of an end-to-end suite this exhausts the client's ephemeral ports on Windows.

Seen downstream (0.49.1, Windows 11, lane on this machine): an acceptance profile with a static SPA and forward = { "/api" = "{service.api.url}", "/file" = "{service.api.url}" }, Playwright with 4 workers, ~1000 tests in 11 minutes. Right after the run:

count
TCP connections in TIME_WAIT to the API's port 6946
ESTABLISHED to the API's port 0
TIME_WAIT to the static site's port (browser side) 1219
dynamic port range (netsh int ipv4 show dynamicport tcp) 49152 + 16384

During the run the browser and the forward started to fail with errors that look like the app's:

  • page.goto: net::ERR_ADDRESS_IN_USE at http://127.0.0.1:<site>/…
  • page.goto: net::ERR_FAILED
  • 502 from the forward on API calls (PUT /api/…), Response has been disposed, Target page, context or browser has been closed

17 tests failed and 29 were flaky. The same suite behind a Caddy reverse proxy (which keeps upstream connections alive) ran green on an earlier commit (0 failed, 0 flaky); a dev server's proxy (ng serve) also reuses its upstream connections. Not every one of the 17 is necessarily the forward's, but the connection errors above are.

Expected

The forward keeps a pool of keep-alive connections per target (as a reverse proxy does by default) and reuses them, so the number of connections to the target stays near the number of concurrent requests and a long run does not drain the port range. Streaming responses (server-sent events) and WebSocket upgrades keep their own connection, as now.

Notes

  • TIME_WAIT on the side that closes first: if the forward closes after each response (Connection: close or dropping the client), every request leaves a socket for 2 × MSL (120 s on Windows by default). At ~60 requests/s that is ~7000 sockets, the order measured above.
  • A check that would have caught it: after N requests through a forward, the count of connections to the target is bounded (e.g. a test with a local target counting accepted connections).

Metadata

Metadata

Assignees

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