Skip to content

server: idle connections give their slots up under pressure, one address holds a quarter at most - #3048

Merged
ashhart merged 3 commits into
mainfrom
security/connection-slots
Nov 13, 2026
Merged

ashhart merged 3 commits into
mainfrom
security/connection-slots

Conversation

@ashhart

@ashhart ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

What this changes

Before: 128 connections that sent nothing, or one GET /health each, held every connection slot for up to 15 minutes, with or without keys. After: from three quarters full, a connection with no request in progress closes after 5 seconds, and one remote address holds at most a quarter of the slots.

  • Issue: n/a. SECURITY.md asks for private reports, so there is no public issue.
  • Stacked on: main.
  • atc overlap --json: not run.

Checks on this head

  • Head commit: f7b697ec9f499a6cdd9aa2a2e64d09bab96b22b6
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 345 on the base, 345 on this head, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig fmt --check on 4 files: passes
    • git diff --check: passes
    • zig build test-http-limits-host test-server-cpu: 8/8 steps succeeded; 1358/1365 tests passed (7 skipped)
    • zig build check-native-cuda-host compile-flashnext-mtp-parity-host compile-glm-native-open: 11/11 steps succeeded
    • zig build test-golden test-server-security test-pull-security: 76/76 steps succeeded; 27/27 tests passed
  • New tests:
    • security: one remote address holds at most its share of the connection slots
    • security: local clients share one address and are never limited by it
    • security: connections with no request give their slots up from three quarters full
  • CI: runs on this push.
  • Not run, and why: Not run on Linux or Windows. The Linux server compiles. zig build test and every step that runs on a GPU were not run: this Mac's GPU was serving a model, and no CUDA, ROCm or Intel hardware was available.

Receipt

n/a. This change cannot reach inference: it is the HTTP accept path, before any request is read.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. Main with only this branch's tests added, zig build test-server-security: exit 1. FAIL connections with no request give their slots up under pressure: no slot came free within 12 seconds.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

Notes for the reviewer

  • Local clients share one address and are not counted by it.
  • A reverse proxy on another host is one address, so it gets a quarter of the slots: 32 of the default 128. That is a behavior change worth a decision before merge.
  • The three numbers (5 seconds, three quarters, one quarter) are constants in http_admission.zig, with no flag.
  • server.zig is 597 lines with this change; the new logic sits in http_admission.zig.

Suggested changelog line

Server: under pressure a connection with no request in progress gives its slot up after 5 seconds, and one remote address holds at most a quarter of the connection slots.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review: not ready. In-flight requests are safe (shedding only runs while a connection is waiting for its next request, so SSE streams and slow readers are never cut), but two parts throttle legitimate deployments.

  1. The per-address cap applies with the server nearly empty. One address may hold a quarter of the connections (32) at any load, and only loopback is exempt. Clients that share one address include:

    • Docker's port publishing (-p 8080:8080 in docs/evaluating.md), where host clients arrive from the bridge gateway;
    • reverse proxies, ingress controllers and load balancers;
    • office NAT.

    Connection 33 gets a 503. Apply the cap only under pressure, from 96 of 128 connections. Add flags for the cap (0 = off), for --max-connections, and for a trusted-proxy list treated like loopback.

  2. Idle keep-alive connections close after 5 s once 96 are open, loopback included. A pooled client that reuses one at that moment gets a reset on a POST, which common HTTP clients don't retry. Shed idle connections only at the limit, or exempt loopback.

  3. IPv6 addresses are keyed per /128, so the cap barely limits a client with a /64. Key IPv6 by /64.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner Author

Review fixes pushed (369ab42):

  • The per-address share applies only under pressure, from 96 of 128 open connections. Below that, connections are counted but never refused.
  • New flags:
    • --max-connections (1–1024);
    • --max-connections-per-address (0 turns the share off);
    • --trusted-proxy (repeatable, exact address). A trusted proxy is never counted, like loopback.
  • Idle connections give up a slot only when every slot is taken, so a pooled keep-alive client below the limit never gets a reset.
  • IPv6 is counted by /64.
  • The README, RUNBOOK and api.md describe the flags.

Results:

  • test-connection-admission-host and test-http-limits-host: 62/62 tests.
  • test-server-security and test-server-cpu: 1361/1368 tests (7 skipped). security_check.py: 25/25.

Lean: 345 before and after.

  • Synthena (Ash's Agent)

…ess holds a quarter at most

128 connections that sent nothing, or one /health each, held every slot for up to 15 minutes. From three
quarters full, a connection with no request in progress closes after 5 seconds, and one remote address
holds at most a quarter of the slots. Local clients share an address and are not counted by it.
…or it and trusted proxies

One address's share of the connection slots (a quarter by default) now applies only once 96 of 128 are open; below
that its connections are counted but never refused. --max-connections N (1-1024, default 128),
--max-connections-per-address N (0 turns the share off) and --trusted-proxy ADDRESS (repeatable; exact addresses never
counted, as loopback is not) set the rule. A connection with no request in progress gives its slot up only with every
slot taken, so a pooled client below the limit keeps its connection. IPv6 peers are counted by their /64.
@ashhart
ashhart force-pushed the security/connection-slots branch from 369ab42 to 38ab46c Compare November 13, 2026 02:03
@ashhart
ashhart merged commit 64749dc into main Nov 13, 2026
13 of 18 checks passed
@ashhart
ashhart deleted the security/connection-slots branch November 13, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant