Skip to content

3.x.x: listen with a 4096 backlog (PRISM_STRATUM_LISTEN_BACKLOG) instead of mio's 128, so a reconnect burst doesn't overflow the accept queue - #723

Merged
RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/stratum-listen-backlog
Oct 6, 2026
Merged

RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/stratum-listen-backlog

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

rc.4 candidate. Do not merge during the rc.3 freeze.

On the pair, N2's C5 run opened 400 Stratum sessions at once, with no ramp, through HAProxy 3350 to A:3340. Three things went wrong:

  • Inside A's frontend container, ss -ltn showed a Send-Q of 128, the listen backlog, on 0.0.0.0:3340, 3341 and 4334. That was despite net.core.somaxconn = 4096 in that network namespace.
  • nstat counted TcpExtListenOverflows = TcpExtListenDrops = 175: the accept queue overflowed and the kernel dropped SYNs.
  • HAProxy logged L4 connection failures, and observe layer4 error-limit 2 on-error mark-down marked A down for about 4 s. 158 of the 400 sessions landed on the remote backup frontend and stayed there.

The 128 doesn't come from the kernel or from our code. tokio::net::TcpListener::bind creates its socket with mio's TcpListener::bind, and mio 1.2 listens with a fixed backlog of 128 to match the standard library (mio/src/net/tcp/listener.rs, "Use the same backlog value as the standard library"). Every native listener inherits it. The 2.x.x Python runtime read PRISM_STRATUM_LISTEN_BACKLOG, with a default of 1024, but the native port retired that name, so 3.x.x silently went from 1024 to 128.

This PR binds every listener through a small helper, listen::bind_listener. It does what TcpListener::bind does:

  • it resolves the address and tries each result in order, the first that listens winning and otherwise the last error returned;
  • it sets SO_REUSEADDR as mio does, and leaves IPV6_V6ONLY at the system default;

but it listens with an explicit backlog through tokio::net::TcpSocket.

  • Both Stratum listeners (the primary and high difficulty) take PRISM_STRATUM_LISTEN_BACKLOG. The name comes back from the retired inventory, with a default of 4096. It accepts 1 through 2147483647, the range listen(2) takes, and check-config validates it with the other Stratum settings.
  • The kernel caps the backlog at net.core.somaxconn without an error. The PRISM listening line now logs listen_backlog and the namespace's somaxconn, and a warning names the cap when somaxconn is the lower. The pair's frontend namespace has 4096.
  • The audit/operator HTTP listener and the public read service listen with a fixed 1024, the 2.x.x default. Their clients are health checks, scrapes and API readers, not miners reconnecting at once, so a second knob would be configuration without a use.

The setting is documented in docs/prism-configuration.md, listed in native-settings.txt (and removed from retired-settings.txt), and passed through compose.yaml and .env.example.

Tests:

  • listen::tests::a_burst_beyond_mios_128_fits_the_configured_backlog binds through the helper with a backlog of 512 and never accepts. It then opens 300 connections concurrently, each with a 2 s timeout, and requires all 300 to complete. With nothing accepting, a connect completes only while the accept queue has room; once the queue is full, the kernel drops the SYN.
    • A host whose somaxconn is below 512 fails the test with a message saying so, rather than passing without proof.
    • Before and after: a scratch program ran the test's burst against tokio's TcpListener::bind, which every listener used before this PR, and against TcpSocket::listen(512), which the helper uses. On this host (net.core.somaxconn = 4096) it got 133 of 300 and 300 of 300, the same on both runs.
  • listen::tests::an_address_that_cannot_be_bound_reports_the_bind_error checks that an address in use returns the bind error, as TcpListener::bind does.
  • config_cli::stratum_listen_backlog_is_checked_against_the_listen_range checks that check-config:
    • rejects 0 and 2147483648 with the range message;
    • rejects -1 and 4k as invalid PRISM_STRATUM_LISTEN_BACKLOG;
    • accepts empty, 1, 4096, 65536 and 2147483647.

Local runs:

  • cargo test -p qbit-prism-server --lib -- listen:: stratum::: 30 passed.
  • cargo test -p qbit-prism-server --test config_cli (the new test and its Stratum and inventory neighbours): 7 passed.
  • cargo clippy -p qbit-prism-server --lib --bins --tests -- -D warnings and cargo fmt --check are clean.
  • python3 scripts/check_prism_settings.py and python3 -m unittest tests.test_check_prism_settings pass.

Review findings left out of this PR:

  • A sysctls: net.core.somaxconn entry in compose.yaml. Kernels since 5.4 give every network namespace 4096 already, as the pair's has. A compose sysctl would also fail under host networking and some hardened runtimes. The new startup line and warning make a lower cap visible instead.
  • Pinning the setting in the load harness's frontend environment, and recording it in capacity evidence. Both are reasonable follow-ups. The harness inherits the compiled-in default, and adding a configuration key to capacity evidence changes the evidence schema, which belongs with 3.x.x: Close the configuration plumbing gaps and harden the container posture #288's validator work, not an rc.4 fix.

Cutover note. Union averages about 20 sessions (p99 72, max 174), and the pair's LB-side mitigation and a connect ramp cover the cutover itself. This restores the headroom 2.x.x had and removes the 128-connection cliff behind any reconnect burst.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

…ead of mio's 128

tokio's TcpListener::bind listens with mio's fixed backlog of 128, the
standard library's value, so every native listener queued at most 128
connections before accept. On the pair, 400 simultaneous Stratum
connects overflowed that queue 175 times (TcpExtListenOverflows), and
HAProxy's layer-4 observation marked the frontend down. The 2.x.x
runtime read PRISM_STRATUM_LISTEN_BACKLOG with a default of 1024; the
native port had retired the name.

listen::bind_listener binds as TcpListener::bind does (every resolved
address in order, SO_REUSEADDR as mio sets it) but listens through
tokio's TcpSocket with an explicit backlog. Both Stratum listeners take
PRISM_STRATUM_LISTEN_BACKLOG, default 4096, 1 through 2147483647 (the
listen(2) range), validated with the other Stratum settings. The kernel
caps it at net.core.somaxconn without an error, so the "PRISM listening"
line logs the backlog and the namespace's somaxconn, and a warning names
the cap when somaxconn is lower. The audit/operator and public HTTP
listeners use a fixed 1024. The setting moves from the retired inventory
to the native one, with docs, .env.example and compose passthrough.

Tests: a burst of 300 connects against a never-accepting listener with
backlog 512 must all complete (133 of 300 complete against mio's 128),
an address in use reports the bind error, and check-config enforces the
range and the parse.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-06T12:02:51.677888Z 0b20117 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@RobertClarke
RobertClarke merged commit cdec405 into 3.x.x Oct 6, 2026
40 checks passed
@RobertClarke
RobertClarke deleted the RobertClarke/stratum-listen-backlog branch October 6, 2026 13:17
RobertClarke added a commit that referenced this pull request Oct 6, 2026
…ace, and the rollback hazard of the always-passed backlog setting

From the review of #725:
- The known issues add #724 to rc.3's list rather than claiming the rc.3
  notes had it.
- The pair evidence carried from rc.3 is named as rc.3's notes name it:
  the D1 peak-second gate, the 750/s ceiling and the failover drill run 1
  on #719's build. "C10" is in no record.
- Rolling a frontend back to rc.3 or earlier needs
  PRISM_STRATUM_LISTEN_BACKLOG removed first. This candidate's
  compose.yaml always passes it, and earlier builds in production mode
  refuse a set-but-unread PRISM_* setting.
- rc.1 to rc.3 did not read the setting: they refused it in production,
  and elsewhere ignored it and listened with mio's 128. A carried 2.x.x
  value overrides the 4096 default.
- #723's deployment-surface changes (compose.yaml, .env.example) are
  listed. Its CI proof is the unit test, and the pair's C5 re-run is the
  proof on the pair.
- The log line is the primary listener's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 6, 2026
Set VERSION and [workspace.package].version to 3.0.0-rc.4: rc.3 plus #723
(the Stratum listeners listen with PRISM_STRATUM_LISTEN_BACKLOG, default
4096, instead of mio's fixed 128). Both Cargo.lock files move only their
workspace-member entries.

doc/release-notes-3.0.0-rc.4.md covers:
- the change and the pair's C5 finding (175 accept-queue overflows in a
  400-connect burst, frontend A marked down, 158 sessions spilled to the
  backup);
- upgrading: the setting is read again (rc.1 to rc.3 did not read it),
  no migration, somaxconn at least as high as the backlog, and removing
  the always-passed setting before rolling a frontend back to rc.3 or
  earlier;
- the same evidence set as rc.3, with the pair evidence rc.3's notes cite
  carried;
- the known issues carried from rc.3 and the #724 flake.

The development-line notes name 3.0.0-rc.4, and they and the 3.0.0 notes
link all four candidates' notes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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