Skip to content

fix: evict least-active sessions when accept queue is full - #1192

Merged
FlorentinDUBOIS merged 3 commits into
mainfrom
fix/accept-queue-eviction
Apr 29, 2026
Merged

FlorentinDUBOIS merged 3 commits into
mainfrom
fix/accept-queue-eviction

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

  • Add evict_least_active_sessions() to SessionManager that selects the oldest 1% of non-listener sessions by last_event() and closes them when the accept queue is full
  • Add evict_on_queue_full config option (default: true) to ServerConfig
  • Integrate eviction into create_sessions() when connection limits are reached

Review fixes applied

  • Deduplicated session-closing logic by delegating to shut_down_sessions_by_frontend_tokens
  • Replaced O(n log n) sort_by_key with O(n) select_nth_unstable_by_key for the hot path
  • Fixed unsafe can_accept = true bypass → proper check_limits() re-validation after eviction
  • Added empty-candidates guard to prevent panic on edge case
  • Fixed test_max_connections e2e test by disabling eviction in test config

Closes #644, closes #916

Test plan

  • cargo test --workspace passes
  • cargo clippy --workspace clean
  • E2e tests pass (20/20), including test_max_connections

Comment thread lib/src/server.rs Outdated

/// Evict the least recently active sessions to make room for new connections.
/// Returns the number of sessions evicted.
fn evict_least_active_sessions(&self, count: usize) -> usize {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function uses two collect, making two memory allocations. Moreover, its time complexity is O(n) where n is current number of sessions, making it very inefficient.
And since it is called for each new connection, it might have a disastrous impact on performance.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I have updated the code, besides, as it run only when the flag is true, and by default it is false (the defensive choice), I think it should okay.

@llenotre llenotre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely performance issues

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/accept-queue-eviction branch from b9011bb to 165fe6e Compare April 28, 2026 14:20
Add an opt-in `evict_on_queue_full` knob (proto `ServerConfig` field
22, default `false`) that, when set, evicts the oldest 1% of
non-listener sessions to make room for queued sockets when
`SessionManager::check_limits` refuses a new accept.

Selection uses `select_nth_unstable_by_key` (introselect, O(n)
average) over `Session::last_event()` to partition the candidate
slab in place, avoiding an O(n log n) sort. The cap loop in
`Server::create_sessions` keeps the existing
`incr!("listener.connection_capped")` counter (so dashboards stay
meaningful regardless of eviction outcome), runs the eviction
batch, and re-checks limits before continuing. A new
`sessions.evicted` counter is emitted only when the mitigation
fires.

Eviction is deliberately skipped during graceful `shutting_down`:
forcing sessions closed there defeats the shutdown semantics and
is wasted work since the worker is winding down anyway.

Default `false` because during a DDoS the active sessions are more
likely to be legitimate clients than the queue contents — evicting
them would serve attackers. Operators dominated by normal traffic
spikes can flip it on.

Closes #644, closes #916.

Signed-off-by: Florentin Dubois <[email protected]>
…h small max_connections

The eviction batch is `(max_connections / 100).max(1)` — a 1%
ratio by design. Below 100 connections the floor of 1 means each
cap event evicts a larger share than 1% of capacity (~4% at
max_connections=25, ~10% at 10), which can surprise an operator
who reads the knob as "1% per round".

Mirror the `basic_auth_max_credential_bytes` "33% of buffer_size"
pattern: emit a `warn!` from `ConfigBuilder::into_config` when
both knobs would interact, so the discrepancy surfaces at boot
rather than under traffic. No hard-error — operators on tiny test
configs may legitimately want eviction.

Signed-off-by: Florentin Dubois <[email protected]>
…evicted

Add the `evict_on_queue_full` row to the global Parameters table
in `doc/configure.md`, the `sessions.evicted` counter row to the
metrics section, and a Keep-a-Changelog `### Added` entry under
`## [Unreleased]` (the main-scoped Unreleased section, ordered
before the existing `### Security` per Keep-a-Changelog
convention).

The doc row calls out the small-`max_connections` warning behaviour
so operators don't have to read source to discover when the 1%
ratio drifts.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/accept-queue-eviction branch from 165fe6e to 04c3edd Compare April 29, 2026 07:33
@FlorentinDUBOIS
FlorentinDUBOIS merged commit f2fb10c into main Apr 29, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/accept-queue-eviction branch April 29, 2026 08:13
FlorentinDUBOIS added a commit that referenced this pull request May 2, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks —
`## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs #1162, #1191, #1192,
#1193, #1194, #1196, #1202, #1223), `## feat/answer-templates-rewrite-auth -
Unreleased` (answer templates, URL rewrite, basic auth), and
`## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR #1209). All
three feature stacks are on `main`; the per-branch dividers were release-
engineering staging that no longer reflects current state.

Changes:
- Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and
  `## feat/h2-mux - Unreleased` dividers; their bodies now appear under
  `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`,
  `### Added — H2 multiplexer (PR #1209)`).
- Promote the breaking changes (proto-tag renumbers, dashboard metric renames,
  `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header
  semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to
  a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of
  the section so operators see them first.
- Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed,
  ✍️ → Changed, 📚 → Documentation, 🤖 → CI, ⚠️ → Behaviour-affecting). Per-
  commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they
  don't conflict with Keep-a-Changelog vocabulary.
- Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim;
  they are released and immutable.

Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut
in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple
`### Added`/`### Changed`/`### Fixed` sub-blocks into one each per
Keep-a-Changelog strict shape; this commit is the structural collapse only.

Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse).
Signed-off-by: Florentin Dubois <[email protected]>
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
…viction

fix: evict least-active sessions when accept queue is full
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
The CHANGELOG previously carried three concurrent Unreleased blocks —
`## [Unreleased]` (main-scoped post-1.1.1 fixes from PRs sozu-proxy#1162, sozu-proxy#1191, sozu-proxy#1192,
sozu-proxy#1193, sozu-proxy#1194, sozu-proxy#1196, sozu-proxy#1202, sozu-proxy#1223), `## feat/answer-templates-rewrite-auth -
Unreleased` (answer templates, URL rewrite, basic auth), and
`## feat/h2-mux - Unreleased` (the H2 multiplexer rewrite from PR sozu-proxy#1209). All
three feature stacks are on `main`; the per-branch dividers were release-
engineering staging that no longer reflects current state.

Changes:
- Drop the `## feat/answer-templates-rewrite-auth - Unreleased` and
  `## feat/h2-mux - Unreleased` dividers; their bodies now appear under
  `## [Unreleased]` with provenance markers (`### Added — Answer templates / ...`,
  `### Added — H2 multiplexer (PR sozu-proxy#1209)`).
- Promote the breaking changes (proto-tag renumbers, dashboard metric renames,
  `http.errors` labelling, pattern-trie regex anchoring, empty-val del-header
  semantic, sub-16393 `buffer_size` rejection, cipher-list constant rename) to
  a top-level `### Breaking changes (v1.1.x → v2.0.0)` paragraph at the top of
  the section so operators see them first.
- Drop emoji from top-level subheaders (🌟 → Added, 🔄 → Changed, ⛑️ → Fixed,
  ✍️ → Changed, 📚 → Documentation, 🤖 → CI, ⚠️ → Behaviour-affecting). Per-
  commit-log subsection at depth ≥ 4 keeps minor emoji ornaments where they
  don't conflict with Keep-a-Changelog vocabulary.
- Preserve historical release entries (`## 1.1.1`, `## 1.1.0`, …) verbatim;
  they are released and immutable.

Net effect: a single `## [Unreleased]` block ready for the v2.0.0 release-cut
in §3.4.c of `tasks/todo.md`. Future passes can de-duplicate the multiple
`### Added`/`### Changed`/`### Fixed` sub-blocks into one each per
Keep-a-Changelog strict shape; this commit is the structural collapse only.

Refs: tasks/todo.md §3.1.a; codex M12 amendment (3-way not 2-way collapse).
Signed-off-by: Florentin Dubois <[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.

worker not restart, (max number of session connection reached, flushing the accept queue) Drop old connections when the accept queue is full

2 participants