Skip to content

docs(issues): shutdown process — analysis, research, feature definition and sub-issue specs - #1993

Merged
josecelano merged 16 commits into
torrust:developfrom
josecelano:1488-overhaul-tracker-shutdown-docs
Sep 2, 2026
Merged

josecelano merged 16 commits into
torrust:developfrom
josecelano:1488-overhaul-tracker-shutdown-docs

Conversation

@josecelano

@josecelano josecelano commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

Summary

This draft PR defines the documentation-first refactor plan for EPIC #1488, Overhaul: Tracker Shutdown. It contains no production Rust changes.

Selected architecture

The target is a supervised cancellation tree:

  • executable entry points alone translate SIGINT/SIGTERM (and supported Windows console events) into an in-process shutdown request;
  • JobManager supervises direct named top-level components through a root CancellationToken;
  • components own, cancel, and join or deliberately abort their nested tasks before reporting outcomes upward;
  • cancellation flows top-down while completion, failure, timeout, and abort outcomes flow bottom-up through awaited handles; and
  • server libraries expose deterministic in-process lifecycle APIs and do not subscribe to OS signals in the target design.

Resolved decisions and policy

docs/features/shutdown-process/questions.md records Q1–Q10 as resolved, including:

  • exit code 0 only for a fully graceful shutdown; startup/component failure, panic, timeout, or deliberate abort yields 1;
  • one 25-second process-wide deadline, with 20 seconds for HTTP/REST/health drains and 5 seconds for accepted UDP requests;
  • an external orchestrator grace period of at least 30 seconds, with 35 seconds or more recommended where practical; Docker/Podman's default 10 seconds is insufficient;
  • SIGHUP reload is deferred in favor of normal graceful restart; and
  • readiness becomes not ready before root cancellation; while draining, /health_check returns HTTP 503 without downstream probe fan-out.

Rebase and current-state reconciliation

The branch is rebased onto current develop. The plan now records the actual transitional lifecycle:

  • main.rs still does not coordinate SIGTERM; server libraries can react independently, but the process does not cancel and await every application job;
  • HTTP, REST API, health-check API, and UDP wrappers already forward the manager token to private Halted::Normal channels; the EPIC replaces this bridge rather than introducing it;
  • JobManager waits sequentially with a full 10-second budget per job, aborting and joining timed-out top-level tasks; and
  • UDP IP-ban cleanup is already a separate token-cancellable, JobManager-owned application job, not a per-listener reset-loop child.

Roadmap and issue integration

The EPIC catalog explicitly sequences #1588 inventory revalidation, SI-1 SIGTERM support, and #1586's direct JobManager ownership evaluation, followed by token-aware server, Axum, UDP, standalone-consumer, deprecation/removal, policy, and readiness slices.

Validation

  • git diff --check
  • Shutdown-plan local Markdown link validation
  • linter all
  • TORRUST_GIT_HOOKS_LOG_DIR=.tmp ./contrib/dev-tools/git/hooks/pre-commit.sh

All completed successfully.

Architecture decision record

The repository-wide cancellation-tree decision and its rejected alternatives are now recorded in docs/adrs/20260902074438_adopt_supervised_cancellation_tree_for_shutdown.md.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a structured set of planning artifacts (analysis, research, feature definition, and issue/sub-issue specs) for EPIC #1488 (Overhaul: Tracker Shutdown). It focuses on documenting the current shutdown shortcomings (notably SIGTERM handling) and defining a concrete implementation roadmap without changing production code.

Changes:

  • Introduces docs/analysis/ and docs/research/ conventions and adds initial shutdown-focused analysis/research docs.
  • Adds a feature definition for “Shutdown Process”, including a tracked open-questions backlog.
  • Adds/updates EPIC + sub-issue specification folders (SI-1 … SI-9) with verification templates.

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
project-words.txt Adds new technical terms for spell-checking (needs re-sorting to maintain alphabetical order).
docs/AGENTS.md Adds analysis/ and research/ to the docs navigation table.
docs/index.md Adds new “Analysis Documents” and “Research Documents” sections.
docs/analysis/AGENTS.md Defines purpose + naming/lifecycle conventions for analysis docs.
docs/analysis/20260716-shutdown-process/README.md In-depth code-level analysis of current shutdown behavior and gaps.
docs/research/AGENTS.md Defines purpose + naming/lifecycle conventions for research docs.
docs/research/20260716-console-shutdown-patterns/README.md Research on SIGINT/SIGTERM semantics and common Rust/Tokio patterns.
docs/features/shutdown-process/README.md Feature definition for shutdown behavior + design ideas.
docs/features/shutdown-process/open-questions.md Open-questions tracker for unresolved design/contract decisions (Q1–Q10).
docs/issues/open/1488-overhaul-tracker-shutdown/ISSUE.md Local EPIC spec tying analysis/research/feature docs to a sub-issue plan.
docs/issues/open/1588-review-shutdown-process-for-all-tasks-jobs/ISSUE.md Local spec for sub-issue #1588 (inventory + migration planning).
docs/issues/open/1588-review-shutdown-process-for-all-tasks-jobs/verification.md Verification evidence template for #1588.
docs/issues/drafts/1488-si-1-add-sigterm-to-main/ISSUE.md Draft SI-1 spec: add SIGTERM handling to main.rs.
docs/issues/drafts/1488-si-1-add-sigterm-to-main/verification.md SI-1 verification template incl. pre-implementation evidence.
docs/issues/drafts/1488-si-2-remove-global-shutdown-signal/ISSUE.md Draft SI-2 spec: remove per-server global signal shutdown path.
docs/issues/drafts/1488-si-2-remove-global-shutdown-signal/verification.md Verification template for SI-2.
docs/issues/drafts/1488-si-3-fix-environment-stop/ISSUE.md Draft SI-3 spec: fix standalone examples’ shutdown behavior.
docs/issues/drafts/1488-si-3-fix-environment-stop/verification.md Verification template for SI-3.
docs/issues/drafts/1488-si-4-migrate-torrent-cleanup/ISSUE.md Draft SI-4 spec: migrate torrent cleanup job to CancellationToken.
docs/issues/drafts/1488-si-4-migrate-torrent-cleanup/verification.md Verification template for SI-4.
docs/issues/drafts/1488-si-5-migrate-activity-metrics-updater/ISSUE.md Draft SI-5 spec: migrate activity metrics updater to CancellationToken.
docs/issues/drafts/1488-si-5-migrate-activity-metrics-updater/verification.md Verification template for SI-5.
docs/issues/drafts/1488-si-6-align-grace-periods/ISSUE.md Draft SI-6 spec: align JobManager grace periods with Axum drain timeouts.
docs/issues/drafts/1488-si-6-align-grace-periods/verification.md Verification template for SI-6.
docs/issues/drafts/1488-si-7-observable-shutdown-progress/ISSUE.md Draft SI-7 spec: add observable shutdown progress logging.
docs/issues/drafts/1488-si-7-observable-shutdown-progress/verification.md Verification template for SI-7.
docs/issues/drafts/1488-si-8-configurable-grace-periods/ISSUE.md Draft SI-8 spec: add [shutdown] config section for grace periods.
docs/issues/drafts/1488-si-8-configurable-grace-periods/verification.md Verification template for SI-8.
docs/issues/drafts/1488-si-9-improve-udp-shutdown/ISSUE.md Draft SI-9 spec: improve UDP shutdown observability/draining decisions.
docs/issues/drafts/1488-si-9-improve-udp-shutdown/verification.md Verification template for SI-9.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread project-words.txt Resolved
@josecelano

Copy link
Copy Markdown
Member Author

timeout 20s cargo run does not work, the tracker is not shut down. Add it as a acceptance criteria.

josecelano added a commit that referenced this pull request Aug 24, 2026
af5ddb7 fix(integration): tighten fixture ownership (Jose Celano)
aeab815 test(integration): add application lifecycle fixture (Jose Celano)
4c5f09a docs(issues): plan #1419 completion (Jose Celano)

Pull request description:

  ## Summary

  Add `TrackerApplicationFixture` for main-application integration suites. The fixture owns the isolated workspace, application container, and `JobManager`, then invokes the best shutdown sequence exposed by current production before releasing the workspace.

  - Migrate the current metrics, UDP-policy, banning, and scaffold suites from implicit `_jobs` drop cleanup.
  - Add focused lifecycle ordering coverage: the workspace is released only after awaited fixture shutdown.
  - Reconcile #1419 documentation and record the verification evidence.

  ## Verification

  - `cargo test --test metrics-fixed-ports --test metrics-port-zero --test metrics-udp-error-enabled-port-zero --test metrics-udp-error-disabled-port-zero --test banning-udp-metrics-disabled-port-zero --test scaffold`
  - `cargo test --test metrics-port-zero -- --nocapture`
  - `cargo test --test metrics-port-zero -- --test-threads=1`
  - `TORRUST_GIT_HOOKS_LOG_DIR=.tmp ./contrib/dev-tools/git/hooks/pre-commit.sh --format=json`

  ## Scope and Follow-up

  Partially addresses #1419; it intentionally remains open.

  Current server jobs can consume `JobManager::wait_for_all`'s per-job timeout because they do not yet react to the shared cancellation token. This change makes test lifecycle ownership explicit but does not claim cooperative server completion. Production shutdown coordination is deferred to #1488 and draft PR #1993; this PR does not introduce a competing shutdown mechanism.

ACKs for top commit:
  josecelano:
    ACK af5ddb7

Tree-SHA512: 14a25b414891ff0fe032293c3af59532c7ce7d1d206dfdf075acb930653805a8f8019f5e8049c250ff91eee6f690216dd3ec1b337bb1f5ad729e8955fef98b4f
@josecelano
josecelano force-pushed the 1488-overhaul-tracker-shutdown-docs branch from 6a3930f to 8ccf459 Compare September 2, 2026 07:18
@josecelano

Copy link
Copy Markdown
Member Author

Addressing the earlier timeout 20s cargo run request in commit 5e8807cc.

timeout 20s cargo run is not a valid shutdown acceptance criterion because it signals Cargo's launcher process, which may differ from the tracker process. SI-1 now requires bounded signal verification against the direct release-binary PID. A harness may use timeout only to bound the test and must allow for the current sequential legacy shutdown path; SI-20 owns the final 25-second process deadline.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Several issue-spec frontmatter YAML blocks contain a real indentation error (and some new issue metadata deviates from the documented template), which can break documentation tooling and should be corrected before merge.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 63/63 changed files
  • Comments generated: 5
  • Review effort level: Lite

josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Sep 2, 2026
josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Sep 2, 2026
@josecelano
josecelano marked this pull request as ready for review September 2, 2026 08:58
Introduce two new top-level documentation directories:
- docs/analysis/  — in-depth studies of the project's own code,
  typically produced before a refactoring plan or feature definition
- docs/research/  — external research on technologies and patterns

Each folder has an AGENTS.md explaining its purpose and the
timestamp-prefix naming convention (same as ADRs).

docs/AGENTS.md and docs/index.md are updated to register both folders.

Ref: torrust#1488
Analyses the current shutdown implementation across src/main.rs,
JobManager, all job starters, server launchers, and
torrust_server_lib::signals.

Key findings:
- main.rs only handles SIGINT; SIGTERM is silently ignored
- Three inconsistent shutdown mechanisms (CancellationToken,
  direct ctrl_c, oneshot Halted channel)
- Grace period mismatch: JobManager 10s per-job vs Axum 90s
- UDP server uses abort() with no connection draining
- Double-signal race on Ctrl+C

Includes experimental validation (2026-07-16, commit 49d8117):
confirmed kill <pid> (SIGTERM) is ignored by main.rs while servers
react independently via global_shutdown_signal().

Ref: torrust#1488
Investigates how long-running Rust services handle OS signals for
graceful shutdown. Covers:
- SIGINT vs SIGTERM vs SIGKILL semantics and differences
- Tokio signal APIs (ctrl_c(), unix::signal(), CancellationToken)
- Real-world patterns from Vector (Datadog): 4-signal handler,
  configurable 60s grace period, two-phase shutdown
- Docker/K8s/systemd grace period reference values
- Clarifies that kill is not brutal: it sends SIGTERM by default

Recommendations for the Torrust Tracker:
- Add SIGTERM handler to main.rs (minimal, highest value)
- Remove global_shutdown_signal() from per-server shutdown
- Make grace periods configurable

Ref: torrust#1488
…sue specs

Feature definition (docs/features/shutdown-process/):
- README.md: product-level description of the shutdown feature including
  Unix/Docker/K8s/systemd contracts, principle of least surprise, user
  value table, production scenarios, shutdown trigger options analysis,
  and design ideas
- open-questions.md: 10 open questions (3 critical, 4 important, 3 minor)
  that must be resolved before implementation begins; Q1 fully detailed
  with findings from running the standalone binary examples

EPIC spec (docs/issues/open/1488-overhaul-tracker-shutdown/):
- ISSUE.md: local spec for GitHub EPIC torrust#1488; updated goal, contracts
  being implemented, and sub-issue table with direct links to draft specs

Sub-issue specs (docs/issues/open/ and docs/issues/drafts/):
- 1588/ISSUE.md: local spec for GitHub issue torrust#1588 with manual
  verification procedure
- SI-1 through SI-9: 9 draft sub-issue specs, each as a folder with
  ISSUE.md (spec + manual verification) and verification.md (evidence
  template); SI-1 includes Phase 1 pre-implementation evidence

SI-1 pre-implementation evidence (2026-07-16, commit 49d8117):
- Confirmed kill <pid> (SIGTERM) is ignored by main.rs
- Servers react via global_shutdown_signal() but main.rs does not
- Process requires kill -9 (exit code 137) to stop

Ref: torrust#1488
…l() removal are sequential

Decision recorded in open-questions.md Q1:

- SI-1 (add SIGTERM to main.rs) can land before SI-2 without breaking
  correctness. The triple-signal scenario after SI-1 is harmless: main.rs
  reacts first, servers' global_shutdown_signal() fires as a no-op.
- SI-1 and SI-2 are separate sequential sub-issues. SI-2 requires a
  coordinated torrust-server-lib release and Q5 resolution.
- SI-3 is fully unblocked: Environment::stop() abort-vs-cancel and SIGTERM
  for standalone examples can land independently of SI-2.

Updated:
- open-questions.md: Q1 marked resolved, decisions recorded, Question to
  Sub-issue mapping table and Sub-issue Readiness table added
- EPIC ISSUE.md: sub-issue table Notes updated (SI-2 blocked by Q5 only,
  SI-3 unblocked)
- SI-2 ISSUE.md: blocked notice updated (Q1 resolved, Q5 remains)
- SI-3 ISSUE.md: unblocked notice added, dependencies updated

Ref: torrust#1488
…server job

Decision recorded in open-questions.md Q2 (Option 3):

Each server job starter watches the CancellationToken from JobManager.
When cancelled, it sends Halted::Normal to its own halt channel and waits
for the server task to finish. JobManager API (JoinHandle<()>) is unchanged.
No changes needed to torrust_server_lib or torrust_tracker_axum_server.

Options 1 and 2 rejected:
- Option 1: halt senders are consumed inside async move closures; extracting
  them would require breaking API changes to all start_job functions
- Option 2: requires touching two external standalone packages unnecessarily

Impact on SI-2: must now also add CancellationToken wiring to each server
job starter alongside the global_shutdown_signal() removal in torrust-server-lib.

Updated:
- open-questions.md: Q2 marked resolved, decision and rationale recorded,
  impact table updated
- SI-2 ISSUE.md: goal, implementation, and acceptance criteria updated to
  include CancellationToken wiring in job starters

Ref: torrust#1488
@josecelano
josecelano force-pushed the 1488-overhaul-tracker-shutdown-docs branch from 6e62f81 to 8c127e9 Compare September 2, 2026 09:39
@josecelano

Copy link
Copy Markdown
Member Author

ACK 8c127e9

@josecelano
josecelano merged commit 2d97273 into torrust:develop Sep 2, 2026
1 check passed
@da2ce7

da2ce7 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Post-merge note: this pull request requested a review from this account and was closed without one at its final head. A post-merge review will not be done: This pull request changed only documentation: the shutdown analysis, research, feature definition and subissue specifications, with no production code changes.

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.

3 participants