Skip to content

fix(command): earliest-deadline poll, cancelled-task routes, stopping-worker close - #1838

Merged
FlorentinDUBOIS merged 7 commits into
mainfrom
fix/command-hub-deadlines-cancel-upgrade
Oct 3, 2026
Merged

FlorentinDUBOIS merged 7 commits into
mainfrom
fix/command-hub-deadlines-cancel-upgrade

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Three fixes in the main-process command hub (bin/src/command/).

Fixes #1826
Fixes #1827

#1832 (pending control commands across upgrade-main) is not addressed here. It is handled separately by a design that transfers pending control commands to the new main process. An earlier revision of this PR refused upgrade-main while a command was pending; that refusal has been withdrawn (see below).

#1826: the command poll sleeps until the latest task deadline

CAUSE: CommandHub::run (bin/src/command/server.rs) selected the poll timeout with .max() over the pending task deadlines. A silent worker produces no readiness, so the earlier task was only reaped when the later deadline expired, and its client was answered late.

Fix: CommandHub::next_poll_timeout uses the earliest outstanding deadline (.min()).

Test: poll_wakes_up_for_the_earliest_task_deadline. Two tasks are scattered to a worker that never answers, with deadlines of 50 ms and 60 s. The test asserts the poll timeout is at most 50 ms. RED on origin/main semantics (.max()): the loop must wake up by the earliest deadline (50ms), got 59.999554694s. This test pins the timeout computation itself. It does not run the infinite run loop against the wall clock: the sweep at the top of that loop already reaps any task whose deadline has passed.

#1827: cancelling a control task leaves its worker-response routes

CAUSE: Server::cancel_task removed the task from queued_tasks only, so its in_flight routes survived. A late worker answer, or fail_in_flight_requests_of_worker after a worker closed, then found the route, found no task, and returned early in handle_worker_response before any cleanup. Each state replay that load_state abandons on a parse error (the only caller) leaked its routes.

Fix: cancel_task also retires every in_flight route of the cancelled task and keeps the routes of other tasks. This matches the purge handle_finishing_task already does.

Test: cancelling_a_task_retires_only_its_response_routes. It cancels one task that has 2 routes while a second task with 1 route is pending, then sends a late answer to the cancelled task and closes a worker. RED: left: [("1-0-1", 0), ("0-1-1", 1), ("0-0-1", 0)] right: [("0-1-1", 1)].

A stopping worker that closes answers its in-flight commands

CAUSE: upgrade --worker marks the old worker Stopping, then waits for its SoftStop answer in a task with no deadline (Timeout::None). The worker-close path synthesised failures only for an is_active worker, which Stopping is not. A Stopping worker whose channel closed before it answered left that task, its in_flight route and its client waiting until the main process restarted.

Fix: CommandHub::on_worker_channel_closed synthesises the failures on the first close of any worker that is not yet Stopped. A repeated CloseSession of the same session, now Stopped, still synthesises nothing, so nothing is double-counted.

Test: a_stopping_worker_closing_finishes_its_pending_task. A Stopping worker holds a SoftStop in a Timeout::None task and closes twice. The test asserts that in_flight is empty, the task has finished, and it accounts exactly one failure (0, 1, 1). RED with the is_active gate restored: the task stays unfinished (panic at the has_finished assertion).

Withdrawn: the upgrade-main refusal

The refusal (upgrade_main failing while CommandHub::pending_task_count was non-zero) broke the #1668 e2e main_upgrade_keeps_a_draining_worker_serving_its_requests (bin/tests/upgrade_keeps_draining_worker_e2e.rs). A main upgrade while a worker is still draining after upgrade --worker 0 must succeed, and the draining worker's SoftStop task kept the upgrade refused. The refusal, pending_task_count, its test, and its documentation in doc/configure_cli.md, bin/src/command/LIFECYCLE.md and CHANGELOG.md have been removed. The test that pinned the refusal was deleted because the behavior itself is withdrawn; no check was silenced.

Validation

  • cargo test -p sozu -p sozu-command-lib --all-features --locked -> exit 0 (150 bin tests, 237 command-lib tests, all passed)
  • cargo test -p sozu --all-features --test upgrade_keeps_draining_worker_e2e -- --ignored -> main_upgrade_keeps_a_draining_worker_serving_its_requests ... ok
  • cargo clippy --all-features -p sozu -- -D warnings -> exit 0
  • cargo +nightly fmt --all --check -> exit 0
  • python3 .github/scripts/check_doc_citations.py --root . --base origin/main -> exit 0

The main-process event loop computed its poll timeout from the latest
deadline of the pending tasks. A silent worker produces no readiness, so
with two worker-backed commands pending, the one due first was only
reaped, and its client only answered, once the later deadline expired.

CommandHub::next_poll_timeout now selects the earliest outstanding
deadline. poll_wakes_up_for_the_earliest_task_deadline pins it with two
tasks held by a silent worker (deadlines 50 ms and 60 s).

Fixes #1826

Signed-off-by: Florentin Dubois <[email protected]>
Server::cancel_task dropped the queued task but left its in_flight
routes. A late worker answer or a worker closing then resolved to the
cancelled task, found no task, and returned before any cleanup, so every
state replay load_state abandons on a parse error leaked its routes for
the life of the main process.

Cancellation now retires every route belonging to the cancelled task and
keeps the routes of other tasks. cancelling_a_task_retires_only_its_response_routes
pins it, including a late answer and a worker closure after cancelling.

Fixes #1827

Signed-off-by: Florentin Dubois <[email protected]>
UpgradeData carries no client, task or worker-response route, and the
old main process stops reading worker answers once the handoff is
confirmed, so a command admitted before the upgrade had no continuation
and its client was never answered.

upgrade_main now refuses while CommandHub::pending_task_count is
non-zero. The requesting client receives an immediate Failure naming the
number of pending commands, before any side effect: no boot_generation
bump, no MainUpgraded audit success, no fork. The pending commands
complete normally and the operator retries. The contract is documented
in doc/configure_cli.md and bin/src/command/LIFECYCLE.md, and pinned by
upgrade_main_is_refused_while_a_control_command_is_pending, which holds
a worker answer across the upgrade request.

Fixes #1832

Signed-off-by: Florentin Dubois <[email protected]>
The old worker of an upgrade --worker is marked Stopping, then its SoftStop
answer is awaited by a task with no deadline. The close path synthesised
failures only for an active worker, so a Stopping worker that closed before
answering left that task unfinished forever, and every later upgrade-main was
refused as pending until the main process restarted.

Synthesise the failures on the first close of any worker that is not yet
Stopped, which still prevents double counting on a repeated CloseSession, and
stop counting tasks that already gathered every answer as pending.

Signed-off-by: Florentin Dubois <[email protected]>
upgrade_main refused to hand over while CommandHub::pending_task_count was
non-zero. That breaks the main-upgrade-during-drain contract pinned by
main_upgrade_keeps_a_draining_worker_serving_its_requests
(bin/tests/upgrade_keeps_draining_worker_e2e.rs): an upgrade --worker whose
old worker is still draining keeps a Timeout::None SoftStop task pending, so
the main upgrade was refused. Pending control commands are to be transferred
to the new main process instead, which issue #1832 tracks separately.

Remove the refusal from upgrade_main, the pending_tasks parameter of
Server::handle_client_request, CommandHub::pending_task_count (no other
caller), the test-helper visibility widening it needed, and the refusal
documentation in doc/configure_cli.md, bin/src/command/LIFECYCLE.md and
CHANGELOG.md.

The test upgrade_main_is_refused_while_a_control_command_is_pending is
deleted deliberately: it pinned the refusal contract that this commit
withdraws, so it no longer describes intended behavior. No check is
silenced; the e2e above passes again.

The earliest-deadline poll (#1826), the cancelled-task route retirement
(#1827) and the Stopping-worker close fix stay. The latter's test is renamed
a_stopping_worker_closing_finishes_its_pending_task and now asserts that the
task finishes with one synthetic failure and that in_flight is empty, with
no reference to the withdrawn refusal; its CHANGELOG entry stands alone.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS FlorentinDUBOIS changed the title fix(command): earliest-deadline poll, cancelled-task routes, upgrade-main with pending commands fix(command): earliest-deadline poll, cancelled-task routes, stopping-worker close Oct 3, 2026
@FlorentinDUBOIS
FlorentinDUBOIS merged commit d7b5f14 into main Oct 3, 2026
28 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 4, 2026
The main-process hot upgrade rebuilt an empty command Hub while worker work could still be in flight. The original command client then received EOF, and a later worker response reached the replacement as an unknown task even though its data-plane request could still finish.

This change transfers the complete control-plane ownership graph to the replacement main: connected clients, client and worker channel buffers, active and queued tasks, worker-response routes, subscribers, worker pending queues, stopped workers still referenced by a task or route, task deadlines, audit clocks, and future-ID counters. The replacement restores task state directly without replaying operator requests or re-scattering work.

The handoff uses an explicit V2 `PREPARED` / `COMMIT` / `ACTIVATED` protocol:

- the replacement validates and registers inherited descriptors while paused, without command or worker data I/O;
- pre-commit rejection closes and reaps the candidate, restores `CLOEXEC`, and lets the unchanged old Hub continue;
- the first attempt to send `COMMIT` irreversibly fences the old Hub, so an ambiguous post-commit failure cannot execute work twice;
- after a complete commit, the replacement activates the Hub, schedules restored sessions once, publishes PID and systemd readiness, preserves the initiating client's terminal response under backpressure, and acknowledges activation before the old main exits.

Compatibility fails closed before adoption. A running main from before this protocol cannot export the missing Hub state, so the first deployment requires a controlled service restart after pending commands are drained and state is saved. Subsequent V2-to-V2 hot upgrades transfer pending commands. A V2 sender rejects a legacy candidate before freezing live ownership; a legacy sender remains subject to its existing generation, audit, and child-reaping limitations when a V2 candidate refuses its old envelope.

`SIGTERM` observed before commit aborts and reaps the candidate, then the old event loop consumes the preserved stop intention. Operators should signal the service control group. A signal aimed only at the old PID after the final pre-commit observation remains outside this contract, and a failure after commit is fail-stop rather than crash recovery.

The branch is based on `9561b24e61c23511326ffeb547b0b7fe51ee7055`, including the deadline and cancelled-route fixes from #1838 and the PROXY protocol corrections merged in #1844.

Channel snapshot validation accepts all five defined `Ready` bits in both serialized readiness and interest, preserves them exactly, and rejects the first unknown bit `0x20`. This covers the Linux readiness word `WRITABLE | HUP | WRITE_CLOSED` (`0x001a`) without special-casing that observed value.

Descriptor-cleanup tests observe the original command-channel and SCM socket peers instead of probing released numeric descriptors or requiring immediate EOF. EOF is the only success outcome within a fixed one-second bound: data, other errors, and expiration fail. This tolerates transient descriptor copies across a concurrent fork-to-exec window while retaining a durable duplicate of either endpoint still fails the test. The exact transient holder in the original CI jobs was not instrumented, so that scheduler-level cause remains unverified.

Validation completed locally with Rust 1.93.1 and at most four build jobs:

- `cargo test -p sozu-command-lib --locked`: 244 unit tests, 9 integration tests, and 2 doctests passed; 3 existing doctests were ignored.
- `cargo test -p sozu --locked --test upgrade_fd_inheritance_e2e -- --ignored`: failed 0/1 on the earlier snapshot mask with `invalid channel snapshot readiness bits: 0x001a`, then passed 1/1 after accepting `WRITE_CLOSED`.
- the snapshot regression exercises all 32 subsets of the five defined bits in both readiness and interest; it rejects `0x20` in each field. Removing `WRITE_CLOSED` makes the test fail on `0x10`.
- `cargo test -p sozu --locked --no-default-features --features crypto-openssl,opentelemetry,splice,simd --verbose`: 152/152 library tests passed and every integration and doctest target completed successfully.
- `cargo test -p sozu --locked --no-default-features --features fips,opentelemetry,splice,simd --verbose`: 152/152 library tests passed and every integration and doctest target completed successfully.
- retaining durable duplicates of both the command channel and SCM endpoint makes each corrected cleanup oracle fail with both EOF deadlines; restoring the candidate makes each targeted test pass 1/1.
- the accepted-handoff, rejected-candidate, SIGTERM-during-PREPARE, and descriptor-inheritance process tests pass 1/1 on the rebased head.
- the compatibility matrix passes 1/1 in both directions between the rebased candidate and the frozen pre-protocol `d7b5f14d` binary. This verifies those two transition paths rather than certifying every historical version pair; the legacy generation, audit, and child-reaping limitations described above remain observable.
- `cargo clippy --all-targets --locked --no-default-features --features crypto-openssl,opentelemetry,splice,simd -- -D warnings` and the equivalent FIPS matrix passed.
- `cargo +nightly fmt --all --check` passed.
- `.github/scripts/check_doc_citations.py --base 9561b24` passed all 37 citations, 1,078 test names, and 15 pinned excerpts.
- `env -u RUST_LOG ... cargo +1.93.1 test -p sozu --locked --features opentelemetry,splice,simd --test sigterm_soft_stop_e2e -- --ignored`: 1/1 passed. The local 27/27 missing-access-log result was caused by ambient `RUST_LOG=warn`, which GitHub CI does not set; the test requires its configured `info` access records.

GitHub CI checks are pending on the rebased commit `a8bdd54b8833060576674a8c2aff0374f6e8083b`.

Fixes #1832



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.

Cancelling a control task leaves its worker-response routes Command poll sleeps until the latest task deadline

1 participant