Skip to content

fix(server): an accept() error no longer ends the server (#200) - #201

Merged
gonzafg2 merged 3 commits into
mainfrom
fix/accept-errors
Oct 13, 2026
Merged

gonzafg2 merged 3 commits into
mainfrom
fix/accept-errors

Conversation

@gonzafg2

@gonzafg2 gonzafg2 commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Summary

An accept() error no longer ends the server. Before, any error leaving the accept loop (accepted? in Server::serve) made the binary exit and drop every session. Under systemd's default soft NOFILE of 1024, about 200 concurrent exec sessions at 5 descriptors each are enough to trigger it: the next accept() fails with EMFILE.

Fixes #200

Motivation

This is a bug fix in the plain-PR lane. ADR-0028 decides the accept loop and its caps but says nothing about accept() errors, and its default cap of 256 connections was supposed to bound descriptors. serve's own doc already limited the error return to failures "at the listener level". This PR makes the code match that.

Raising RLIMIT_NOFILE at startup, option 2 in #200, is not included: it changes the server's resource posture and needs its own decision.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that changes existing behaviour)
  • Documentation only
  • Refactor / chore / CI / build

Changes

  • server.rs: classify_accept_error sorts an accept() error into one of three classes (rustix::io::Errno::from_io_error):
    • Listener (EBADF, ENOTSOCK, EINVAL): the listening socket itself is unusable. The error is returned as before, so a service manager can restart the server.
    • Connection, only for errors known to concern one dequeued connection: ECONNABORTED, ECONNRESET, and the network errors accept(2) passes on (EPROTO, ENOPROTOOPT, EOPNOTSUPP, ENETDOWN, ENETUNREACH, EHOSTDOWN, EHOSTUNREACH). These get a debug and an immediate retry, so a peer cannot stall the loop.
    • Pause, everything else: resources (EMFILE, ENFILE, ENOBUFS, ENOMEM), security-module denials (EACCES, EPERM; claude-review) and unknown errors. These get a warn, and the loop stops accepting for ACCEPT_BACKOFF (1 s).
      • Live connections, task reaping and shutdown keep running, because only the accept arm is disabled.
      • These errors may leave the connection queued while the listener stays readable, so an immediate retry would spin. hyper and axum pause for the same second.
  • CHANGELOG.md: a Fixed entry.

Checklist

  • My commits are signed off under the Developer Certificate of Origin (git commit -s).
  • My commit messages follow Conventional Commits.
  • I have run cargo fmt --all and cargo clippy --workspace --all-targets -- -D warnings locally.
  • I have added or updated tests where appropriate.
  • I have updated documentation (rustdoc, docs/, or README.md) where appropriate.
  • I have added a CHANGELOG.md entry under ## [Unreleased] if this change is user-visible.
  • If this change introduces a new dependency, I have justified it in the PR description and verified it passes cargo deny check. (No new dependency. rustix is already a dependency of the core.)

Security considerations

  • Availability (threat model §5.1.3). Descriptor exhaustion no longer takes the server down; it pauses new connections for a second at a time.
  • Pre-auth descriptors stay bounded by max_half_open, exactly as before.
  • Per-connection failures never pause the loop. A peer that triggers ECONNABORTED/ECONNRESET or a network error on its own connection cannot buy a second of non-accepting with it.
  • No error makes the loop spin. A persistent one, such as a policy denial, is logged at warn once per second.
  • No new unsafe, no new dependency.

Tests

  • server.rs unit test accept_errors_are_classified: each Errno of the three classes, including EACCES/EPERM and a non-OS error in the pause class.
  • tests/accept_resources.rs, a separate binary because it lowers this process's RLIMIT_NOFILE:
    • it fills the descriptor table to its last slot, so the client socket takes that slot and the server's accept() gets EMFILE;
    • it checks the loop is still running;
    • it then frees the descriptors and reads the banner of a fresh connection;
    • finally it shuts down cleanly.
  • With EMFILE fatal again (a local mutation), the test fails with "the accept loop ended on EMFILE".
  • Under a real systemd (the feat(systemd): Type=notify readiness, reference unit and operator guide (ADR-0043) #198 container, LimitNOFILE=40, Type=exec), the reproduction from server: any accept() error ends the server (EMFILE at ~200 exec sessions under systemd's default NOFILE limit) #200 that ended in accept loop failed: Too many open files / result=exit-code now leaves the unit active, logging accept failed: out of resources; pausing accepts three times.
  • macOS: clippy is clean, and all workspace tests pass: 123 unit and 40 integration tests in the core, the new resource test, 6 PTY tests and 34 in the binary.
  • Linux, rust:1.99.0-bookworm in Docker: clippy is clean, and the same counts pass.

Notes for reviewers

Automated reviews

PRs authored by dependabot[bot] (and other bots) do not trigger opencode or Claude Code Review. Codex reviews through the Codex cloud GitHub App (ADR-0037).
They are covered by CI (commit-lint, build (ubuntu-latest) and build (macos-latest) — fmt, clippy, tests, release build —, cargo deny, openssh-interop; cargo audit runs weekly, not per PR) and manual review when needed.

https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW

Any accept() error returned from the accept loop (`accepted?`), and the
binary exited, dropping every session. Under systemd's default soft
NOFILE of 1024, about 200 concurrent exec sessions (5 descriptors each)
are enough: the next accept() fails with EMFILE.

accept() errors are now classified:
- out of descriptors or buffers (EMFILE, ENFILE, ENOBUFS, ENOMEM):
  warn, stop accepting for 1 s (the listener stays readable, so an
  immediate retry would spin), keep serving live connections;
- the listening socket itself is unusable (EBADF, ENOTSOCK, EINVAL):
  return the error, as before, so a service manager restarts the server;
- anything else is one failed pending connection: debug, retry at once,
  so a peer cannot stall the loop.

Tests: accept_errors_are_classified (unit); accept_resources.rs fills
the descriptor table so the server's accept() hits EMFILE, checks the
loop is still running, then frees the descriptors and gets a banner
from a fresh connection. With EMFILE fatal again it fails ("the accept
loop ended on EMFILE"). Under systemd with LimitNOFILE=40 the unit now
stays active and logs "pausing accepts" instead of failing.

Fixes #200

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 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-13T05:06:31.355645Z 4d23a81 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.

@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: 659e8240-e269-49d1-9afe-123ede905519)

github run

Comment thread crates/quantumssh-core/src/server.rs Resolved
…rn one connection

claude-review on #201: a SELinux/AppArmor denial (EACCES/EPERM) fails
accept() on the listener before the connection is dequeued, so the
catch-all "retry at once" would spin at 100% CPU, visible only at debug.

The classes are now:
- Listener (EBADF, ENOTSOCK, EINVAL): fatal, unchanged;
- Connection, only the errors known to concern one dequeued connection
  (ECONNABORTED, ECONNRESET and the network errors accept(2) passes on):
  retry at once, so a peer cannot stall the loop;
- Pause, everything else (resources, policy denials, unknown errors):
  warn and stop accepting for 1 s.

The unit test moves EACCES/EPERM and non-OS errors to Pause.

Refs #200

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[email protected]>
@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: 6285e15a-d8ce-4b9d-bfe5-e33b469b655f)

github run

@claude

claude Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

PR Review: fix(server): an accept() error no longer ends the server (#200)

Decision: 🟢 Approved
Iteration: Review #2 (1 resolved, 0 open)
Lenses applied: Rust under crates/ (server.rs accept loop, integration test), CHANGELOG.md

Executive summary

This PR fixes a real availability bug: under descriptor pressure, accept() returning EMFILE would crash the server and drop every live session. The fix correctly classifies errors into three bands — fatal listener faults, per-connection failures (skip), and resource/policy errors (pause 1 s) — and wires the pause into the existing tokio::select! loop using the same guard pattern as the shutdown path. The logic is sound, the type-state transport is untouched, and the new integration test exercises the exact failure mode. No new dependencies, no unsafe, no legacy crypto, no key-material exposure.

Previous issues

# Issue Status Notes
1 EACCES/EPERM classified as Connection (retry-at-once), causing a CPU spin because these errors fire before accept(2) dequeues the connection resolved Fixed in commit 6dd95a7 — both moved to Pause class

No new findings reach the reporting threshold.

Signed-off-by: Gonzalo Fleming <[email protected]>

# Conflicts:
#	CHANGELOG.md
@opencode-agent

Copy link
Copy Markdown

APIError: Insufficient Balance (request_id: d4f11f88-6b82-4533-bac7-76f98315c576)

github run

@gonzafg2
gonzafg2 merged commit 9629dbd into main Oct 13, 2026
10 of 11 checks passed
@gonzafg2
gonzafg2 deleted the fix/accept-errors branch October 13, 2026 06:05
gonzafg2 added a commit that referenced this pull request Oct 13, 2026
Resolves the conflicts with #197 (squashed ADR-0043: keep this branch's
Accepted status and Links), #201 (Server::serve: its # Errors doc with
this branch's loop_rx signature) and the CHANGELOG Fixed section.

Claude-Session: https://claude.ai/code/session_01HxXXh6HEpJBs7tbuz3NmtW
Signed-off-by: Gonzalo Fleming <[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.

server: any accept() error ends the server (EMFILE at ~200 exec sessions under systemd's default NOFILE limit)

1 participant