Repository navigation
feat(runtime): concurrent accept loop, admission control, graceful shutdown - #129
Conversation
…utdown Implements ADR-0028 (extends ADR-0022; the Phase-1 spawn-and-join becomes a JoinSet-driven concurrent loop): - crates/quantumssh-core/src/admission.rs (new): the four admission checks in the ADR's load-shedding order — three non-mutating cap reads (global 256, half-open 100, per-source 10; IPv6 by /64), the state-mutating token bucket last (burst 10, 1/s) so a cap refusal never grows the per-source map. RAII ConnectionGuard: global slot for the connection's life, half-open slot until userauth success; drop-time prune plus an amortised sweep bound the source map. - server.rs: select! over accept / join_next_with_id / the shutdown broadcast. Refused connections close pre-banner and emit connection.refused (limit as structured field) inside the connection span, mutually exclusive with connection.accepted. Panic JoinErrors log connection.closed with the reason as a structured field and the peer recovered via task id. - Graceful shutdown: the broadcast carries the drain deadline; pre-auth connections disconnect immediately, channel-phase connections run until the deadline and are then cancelled — the ADR-0023 backstops (owned-Child kill, exec.finished drop-guard) fire on the drop. The loop aborts stragglers 2 s past the deadline; a second signal aborts immediately. Drained shutdown returns Ok. - main.rs: sync main building the runtime with max_blocking_threads = 4 x max_connections (ADR-0028 blocking-pool sizing); SIGTERM/ SIGINT listener drives the broadcast; exit 0 on both paths. - config: [limits] section and [server].drain_timeout (integer seconds), config-file-only per RFC-0010; zero values are schema_error. [limits] graduates from the deferred-section list. - tokio binary features: +signal, +sync, +time (signal is the only addition to the workspace allowlist, per the ADR). Verification: 168 workspace tests (17 new: admission unit incl. sweep/prune/IPv6-/64/remainder-accrual, schema, and 6 integration: concurrency, cap and rate refusals pre-banner, pre-auth immediate disconnect, drain-at-deadline). Clippy -D warnings clean. Interop gate PASS. Live smoke: two overlapping OpenSSH sessions served concurrently; SIGTERM with drain_timeout=3 drained a sleep-30 exec in ~4 s, exit 0, connection.closed reasons correct. Claude-Session: https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa Signed-off-by: Gonzalo Fleming <[email protected]>
The #128 pattern: this PR implements every ADR-0028 decision, so the Status flips atomically with the merge. Claude-Session: https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa Signed-off-by: Gonzalo Fleming <[email protected]>
|
No server is currently available to service your request. Sorry about that. Please try resubmitting your request and contact us if the problem persists. |
|
Now I have all the context needed. Here is my review. Review:
|
| Commitment / Rule | Assessment |
|---|---|
| Memory-safe by construction | No unsafe in any changed file. unsafe_code = "forbid" unchanged. |
| Post-quantum by default | No KEX changes; mlkem768x25519-sha256 untouched. |
| Zero legacy | No crypto introduced. |
| Small surface, sharp edges | One new feature (signal), justified in ADR. No new crate deps. |
| Permanently open | No license changes. |
| Pre-auth path (§4.1) | Admission checks run pre-banner; same Mutex<State> locked briefly; no unsafe. Map-growth caveat documented above. |
| Fail closed (§4.1) | Refused connections drop(stream) with zero bytes sent — no banner. Integration tests (per_source_half_open_cap_refuses_with_no_banner, accept_rate_limit_refuses_after_the_burst) verify 0-byte closure. |
| Type-state transport (§4.1) | transport.rs untouched; run_connection unchanged. |
| Audit log fields (ADR-0024) | connection.refused has structured limit field. connection.closed has structured reason. connection.refused and connection.accepted are mutually exclusive within the span. exec.* events unchanged from Phase 1. |
| Key zeroization (§4.3) | No key material in new code. ConnectionGuard's Debug exposes only IpAddr and counts. |
| AEAD-only | No MAC path changes. |
| Strict-kex | Unchanged. |
Summary
One actionable: add #[must_use] to Admission::try_admit. The rest of the PR faithfully implements ADR-0028 end to end: 17 new tests cover the four admission checks (including sub-second token-bucket remainder accrual and the sweep/prune mechanics), 6 integration tests verify concurrent handshakes, pre-banner refusal with zero bytes, and both shutdown paths (pre-auth immediate + authenticated drain-at-deadline with elapsed-time assertion). The ADR's status flip from Proposed to Accepted is correct.
There was a problem hiding this comment.
Pull request overview
Implements Phase-2 runtime changes (ADR-0028) to lift the Phase-1 sequential-connection ceiling by adding a concurrent accept loop with admission control (caps + per-source token bucket) and a graceful shutdown path driven by a drain-deadline broadcast.
Changes:
- Add
admissionmodule implementing ADR-0028 load-shedding order, per-source aggregation, and RAII connection accounting. - Refactor the core accept loop to run connections concurrently, enforce admission limits, and drain/abort on shutdown deadline.
- Extend binary config/runtime wiring (new
[limits], drain timeout, tokio signal support) and add integration tests covering concurrency, refusals (pre-banner), and shutdown behavior.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| docs/adr/0028-phase-2-concurrent-connections-limits-graceful-shutdown.md | Marks ADR-0028 as Accepted with acceptance date/link. |
| crates/quantumssh/src/main.rs | Builds the runtime explicitly, adds shutdown signal listener + drain-deadline broadcast, wires limits/drain timeout into server config. |
| crates/quantumssh/src/config.rs | Adds [limits] and [server].drain_timeout, plus schema validation/tests for zero values and partial limits. |
| crates/quantumssh/Cargo.toml | Enables Tokio signal/sync/time features needed for shutdown broadcast + signal handling. |
| crates/quantumssh-core/tests/accept.rs | Adds integration tests for concurrent serving, admission refusals (no banner), and graceful shutdown drain/cut behavior. |
| crates/quantumssh-core/src/server.rs | Implements concurrent accept loop (select! over accept/join/shutdown), admission gating, and shutdown-driven draining/aborting. |
| crates/quantumssh-core/src/lib.rs | Exposes new admission module. |
| crates/quantumssh-core/src/admission.rs | New admission control implementation with caps, per-source token bucket, pruning, and unit tests. |
| Cargo.lock | Adds lockfile entries needed for Tokio signal support (signal-hook-registry). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Re opencode Finding 1 (
The real protection for the RAII guard is structural: the only call site binds the guard and moves it into the connection task; a dropped-guard bug would fail the Findings 2 and 3 are informational (no action requested) and correctly describe the ADR-0028 accepted posture and the feature delta. The failed |
…l path claude-review on #129: the amortised sweep ran at the top of try_admit, so every cap-refused call advanced the counter and every 64th paid an O(sources) retain under the admission mutex — contradicting the load-shedding order the ADR fixes (refusals stay read-only) and the counter's own name. The sweep now sits after the three cap checks, in the mutating phase: a cap-refused flood never advances it nor pays it. New test pins the invariant: a saturated-cap refusal flood past 2x SWEEP_INTERVAL leaves a prunable lingering entry untouched. Claude-Session: https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa Signed-off-by: Gonzalo Fleming <[email protected]>
|
No server is currently available to service your request. Sorry about that. Please try resubmitting your request and contact us if the problem persists. |
Re-enter the original connection span so peer_addr is inherited, keep only reason on the event at INFO, and use ADR-0028's shutdown reason for drain aborts. Signed-off-by: Gonzalo Fleming <[email protected]>
Review:
|
| Commitment / Rule | Assessment |
|---|---|
| Memory-safe | No unsafe. ✅ |
| Post-quantum | No KEX changes. ✅ |
| Zero legacy | No crypto introduced. ✅ |
| Small surface | One new tokio feature (signal), justified in ADR-0028. No new crate deps. ✅ |
| Permanently open | No license changes. ✅ |
| Pre-auth path (§4.1) | Admission checks are bounded integer ops + O(sources) sweep every 64 cap-passing calls; sweep moved to mutating phase (793e563), so cap-refused calls stay read-only. ✅ |
| Fail closed (§4.1) | Refused connections close pre-banner with zero bytes — verified by per_source_half_open_cap_refuses_with_no_banner and accept_rate_limit_refuses_after_the_burst. ✅ |
| Type-state transport | transport.rs untouched; run_connection unchanged. ✅ |
| Audit log (§4.1, ADR-0024) | connection.closed at INFO with structured reason only (inherits peer_addr from span). connection.refused with structured limit. connection.refused/connection.accepted mutually exclusive. ✅ |
| Key zeroization (§4.3) | No key material in new code. ✅ |
Informational note (no action required)
connection.refused is emitted at warn! level (server.rs:155). ADR-0024's connection.* family (accepted, closed) uses INFO. This is a deliberate operational-visibility choice for a refused connection and ADR-0028 §Consequences (negative #3) explicitly records this event as pending finalization before the 0.1.0 log-schema freeze — the level can be reconsidered then. Consistent with the ADR's accepted posture.
Summary
The code faithfully implements ADR-0028 end to end: concurrent accept loop, four-check admission control in the documented load-shedding order, RAII connection accounting, and graceful shutdown with drain deadline + abort. 168 workspace tests pass (17 new admission + 6 new integration), clippy clean, interop gate passes.
|
I've reviewed all nine changed files against CLAUDE.md, the threat model, ADR-0024, and the five MANIFIESTO commitments. Here is my review: Review:
|
| Commitment / Rule | Assessment |
|---|---|
| Memory-safe by construction | No unsafe in any changed file. unsafe_code = "forbid" unchanged. |
| Post-quantum by default | No KEX changes. mlkem768x25519-sha256 untouched. |
| Zero legacy | No crypto introduced. |
| Small surface, sharp edges | One new tokio feature (signal), justified in ADR-0028. No new crate deps. |
| Permanently open | No license changes. |
| Pre-auth path (§4.1) | Admission checks are pure integer operations under a Mutex. No allocations sized by attacker input. Map-growth caveat documented in ADR-0028 §Consequences. Integration tests verify zero-byte pre-banner closure on refusal. |
| Fail closed (§4.1) | KEX path untouched. Refused connections close without a banner (drop(stream)). |
| Type-state transport | transport.rs and run_connection untouched. |
| Audit log fields (ADR-0024) | connection.closed carries structured reason at INFO; peer_addr inherited from span. connection.refused carries structured limit. connection.accepted at INFO. exec.* fields unchanged. |
| Key zeroization (§4.3) | No key material in new code. ConnectionGuard::Debug exposes only IpAddr + counts. |
| AEAD-only | Unchanged. |
| Strict-kex | Unchanged. |
| Load-shedding order (ADR-0028) | After 793e563, sweep counter incremented and retain run only after all three cap checks pass — cap-refused calls stay read-only. Test cap_refused_calls_never_pay_or_trigger_the_sweep pins this. |
| Per-source aggregation | IPv6 /64, v4-mapped canonicalised. Token bucket with sub-second remainder accrual. Drop-time prune + amortised sweep bound SS map growth. |
| Graceful shutdown paths | Pre-auth → immediate disconnect; authenticated → drain to deadline, cancelled future fires ADR-0023 backstops. Abort at deadline + 2 s grace. Second signal → immediate abort. Both paths exit 0. |
| Blocking-pool sizing | max_blocking_threads = 4 × max_connections — admission control bounds concurrency, not thread-pool starvation. |
Summary
One minor finding: connection.refused at WARN is inconsistent with ADR-0024's INFO level for all connection lifecycle events. The rest of the implementation faithfully discharges ADR-0028: 17 admission unit tests (caps, rate, sweep, IPv6 /64), 6 integration tests (concurrent handshakes, zero-byte pre-banner refusals, pre-auth immediate disconnect, authenticated drain-at-deadline with elapsed-time assertion). The ADR-0028 status flip from Proposed to Accepted is correct.
…draining A fast exec could flush past the byte/packet limit and become ready to close in the same turn; rekey_due was only checked in Running/Idle, so the connection closed under the old keys and byte_threshold_triggers _server_rekey flaked on loaded runners. Signed-off-by: Gonzalo Fleming <[email protected]>
|
Now I have full context on all files and all prior review findings have been addressed. Here is my review: Review:
|
| Commitment / Rule | Assessment |
|---|---|
| Memory-safe by construction | No unsafe in any changed file. unsafe_code = "forbid" unchanged. |
| Post-quantum by default | No KEX changes. mlkem768x25519-sha256 untouched. |
| Zero legacy | No crypto introduced. |
| Small surface, sharp edges | One new tokio feature (signal), justified in ADR-0028. No new crate deps. Cargo.lock delta is signal-hook-registry (standard tokio transitive). |
| Permanently open | No license changes. |
| Pre-auth path (§4.1) | Admission checks: bounded integer ops under Mutex. Sweep counter now in the mutating phase only (793e563 — cap-refused calls stay read-only). Map-growth caveat documented in ADR-0028 §Consequences. Integration tests verify zero-byte pre-banner closure (per_source_half_open_cap_refuses_with_no_banner, accept_rate_limit_refuses_after_the_burst). |
| Fail closed (§4.1) | KEX path untouched. Refused connections drop(stream) — no banner. |
| Type-state transport | transport.rs and run_connection untouched. |
| Audit log fields (ADR-0024) | connection.closed at INFO with structured reason only; peer_addr inherited from span (fce8ca4). connection.refused carries structured limit. connection.refused/connection.accepted mutually exclusive. exec.* fields unchanged. |
| Key zeroization (§4.3) | No key material in new code. ConnectionGuard::Debug exposes only IpAddr + counts. |
| AEAD-only | Unchanged. |
| Strict-kex | Unchanged. |
| Load-shedding order (ADR-0028) | Sweep counter now post-cap-checks (793e563). Test cap_refused_calls_never_pay_or_trigger_the_sweep pins the read-only refusal path. |
| Per-source aggregation | IPv6 /64, v4-mapped canonicalised. Token bucket with sub-second remainder accrual (sub_second_refill_remainders_accrue). |
| Graceful shutdown | Pre-auth → immediate disconnect; authenticated → timeout_at drain, cancelled future fires ADR-0023 backstops (owned-Child kill, exec.finished drop-guard). Abort at deadline + 2 s grace. Second signal → immediate abort. Both paths exit 0. |
Informational note (no action required)
The #[must_use] on try_admit finding from an earlier review was correctly not adopted: Result carries #[must_use] at the type level (let _ = admission.try_admit(...) fails CI with -D warnings regardless). The repo pattern applies #[must_use] to plain-value returns only (auth.rs:176,289,308…), never on Result-returning functions.
Summary
One minor finding: connection.refused at WARN is the sole connection event not at INFO — inconsistent with ADR-0024's family-of-events convention. The rest of the implementation faithfully discharges ADR-0028 end to end: 11 admission unit tests (caps, rate, sweep, IPv6 /64), 3 server join-path tests (panic/abort/span-missing), 5 integration tests (concurrent handshakes, zero-byte pre-banner refusals, pre-auth immediate disconnect, authenticated drain-at-deadline). All 172 workspace tests pass; clippy -D warnings clean; release build succeeds.
Align with ADR-0024's connection lifecycle levels; a cap refusal is routine load-shedding, not a security-policy failure. Signed-off-by: Gonzalo Fleming <[email protected]>
|
Addressed the remaining opencode finding in the latest commit: |
Review:
|
| Commitment / Rule | Assessment |
|---|---|
| Memory-safe (MANIFIESTO #1, §4.1) | No unsafe in any changed file. unsafe_code = "forbid" unchanged. |
| Post-quantum (MANIFIESTO #2) | No KEX changes. mlkem768x25519-sha256 untouched. |
| Zero legacy (MANIFIESTO #3) | No crypto introduced. |
| Small surface (MANIFIESTO #4) | One new tokio feature (signal), justified in ADR-0028. sync/time were already in ADR-0022's allowlist. No new crate deps. |
| Permanently open (MANIFIESTO #5) | No license changes. |
| Pre-auth path (§4.1) | Admission checks are bounded integer ops under Mutex. Sweep now only in the mutating phase; cap-refused calls stay read-only. Drop-time prune + amortised sweep bound map growth. Tests verify zero-byte pre-banner closure (per_source_half_open_cap_refuses_with_no_banner, accept_rate_limit_refuses_after_the_burst). |
| Fail closed (§4.1) | Refused connections drop(stream) — no banner. KEX path untouched. |
| Type-state transport | transport.rs and run_connection untouched. |
| Audit log (ADR-0024) | connection.closed at INFO with structured reason only; peer_addr inherited from span. connection.refused at INFO with structured limit. connection.refused/connection.accepted mutually exclusive. exec.* fields unchanged. |
| Key zeroization (§4.3) | No key material in new code. ConnectionGuard::Debug exposes only IpAddr + counts. |
| AEAD-only | Unchanged. |
| Strict-kex | Unchanged. |
| Load-shedding order (ADR-0028) | Caps 1-3 (read-only) first, rate check (mutating) last. Test cap_refused_calls_never_pay_or_trigger_the_sweep pins it. |
| Graceful shutdown | Pre-auth → immediate disconnect; authenticated → timeout_at drain, cancelled future fires ADR-0023 backstops (Child kill via Drop, exec.finished drop-guard). Abort at deadline + 2 s. Second signal → immediate abort. Both paths exit 0. |
| ADR-0028 status flip | Proposed → Accepted with date and PR link — correct per CLAUDE.md. |
172 workspace tests pass (11 admission + 3 server join-path + 5 integration ADR-0028), release build succeeds.
Point README and the Phase-2 scoping note at #128 (config) and #129 (runtime) so "implemented" is verifiable without relying on stale RFC implementation-PR headers. Signed-off-by: Gonzalo Fleming <[email protected]>
* docs: align README and Phase-2 scoping with landed Phase 1/2 work Phase 1 is complete; Phase 2 has config + concurrent runtime. Drop the stale "no Rust code" status and mark the remaining Usable items. Signed-off-by: Gonzalo Fleming <[email protected]> * docs: cite implementing PRs for landed Phase-2 workstreams Point README and the Phase-2 scoping note at #128 (config) and #129 (runtime) so "implemented" is verifiable without relying on stale RFC implementation-PR headers. Signed-off-by: Gonzalo Fleming <[email protected]> --------- Signed-off-by: Gonzalo Fleming <[email protected]>
What
The runtime/concurrency workstream of #109: implements ADR-0028 end to end and closes the pre-auth availability ceiling accepted in Phase 1 (ADR-0022 §Consequences). ADR-0028 flips to Accepted inside this PR, per the #128 pattern.
admission.rs(new): the four checks in the ADR's load-shedding order — non-mutating cap reads first (global 256 / half-open 100 / per-source 10, IPv6 by /64, v4-mapped canonicalised), the state-mutating token bucket last, so a cap refusal never grows the per-source map. RAIIConnectionGuard(half-open ends at userauth success; drop releases everything, panics included); drop-time prune + amortised sweep bound the map.server.rs:select!overaccept()/join_next_with_id/ the shutdown broadcast. Refusals close pre-banner withconnection.refused(structuredlimitfield) inside the ADR-0024 span, mutually exclusive withconnection.accepted. PanicJoinErrors logconnection.closedwith structuredreasonand the peer recovered via task id.Childkill from M5, the fix(channel): drop-guard so a panic cannot skip the exec.finished audit event #124exec.finisheddrop-guard) fire on the drop, so the audit invariant holds on the shutdown path. Loop aborts stragglers 2 s past the deadline; second signal aborts immediately; exit 0 both paths.main.rs: sync main building the runtime withmax_blocking_threads = 4 × max_connections(the ADR's blocking-pool sizing); SIGTERM/SIGINT listener drives the broadcast.[limits]+[server].drain_timeout— config-file-only per RFC-0010;[limits]graduates from ADR-0029's deferred-section list; zero values areschema_error. Key names match thelimitfield values, so the refusal event points at the knob.signal(the one addition to the workspace allowlist, per the ADR) plussync/timefrom the existing allowlist.Verification
-D warningsclean; release build ok; interop gate PASS.SIGTERMwithdrain_timeout = 3drained asleep 30exec in ~4 s with exit 0, loggingshutdown signal received; drainingandconnection.closed reason=rejected: shutdownfor the cut exec.Threat model
Discharges the three §5.1.3 test handles (rate limits, half-open caps, deadlines — all configurable with documented defaults and exercised by tests). §8.3's non-DoS posture unchanged: saturation lockout and rotating-source map growth remain accepted, as ADR-0028 records.
Implements ADR-0028. Refs #109. Closes the Phase-1 security-review availability ceiling.
https://claude.ai/code/session_01J2SZwLuDm1KjfYyPM8npTa