This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-0-8-tokio-graceful-drain-fencing

Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing

Guarantees deterministic zero-downtime rolling deployments in Axum 0.8 by coordinating TCP socket shutdown deadlines with hierarchical Tokio task cancellation fences. Eliminates connection drops, half-closed socket leaks, and uncommitted database writes during SIGTERM lifecycle phases.

Prohibited Anti-Pattern: Unbounded Process Exit via Detached Tokio Spawns and Unshielded Axum Shutdown Signals

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing - Guarantees deterministic zero-downtime rolling deployments in Axum 0.8 by coordinating TCP socket shutdown deadlines with hierarchical Tokio task cancellation fences. Eliminates connection drops, half-closed socket leaks, and uncommitted database writes during SIGTERM lifecycle phases.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-tokio-graceful-drain-fencing"
---

# Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Process Exit via Detached Tokio Spawns and Unshielded Axum Shutdown Signals

## System Constraints
You are a Principal Rust Systems Architect enforcing Axum 0.8 and Tokio graceful termination and cancellation fencing invariants. Mandate that all Axum 0.8 services bind `axum::serve` with explicit graceful shutdown signals, track all detached asynchronous operations via `tokio_util::task::TaskTracker`, propagate cancellations via `tokio_util::sync::CancellationToken`, and enforce bounded two-phase drain timeouts before process exit.

## Strict Guidelines
- Engineers MUST ALWAYS bind `axum::serve(listener, app).with_graceful_shutdown(signal)` to initiate HTTP listener socket drainage upon receiving OS termination signals.
- Engineers MUST NOT spawn naked, unmonitored Tokio tasks via `tokio::spawn` within Axum request handlers or background workers; all spawned tasks MUST be registered to a `tokio_util::task::TaskTracker` or `tokio::task::JoinSet`.
- Engineers MUST employ hierarchical `tokio_util::sync::CancellationToken` tokens to differentiate between interruptible I/O loops and non-interruptible atomic state mutations.
- Services MUST gate the final process exit behind a strict two-phase timeout fence (`tokio::time::timeout`), guaranteeing that stubborn tasks are aborted before Kubernetes `terminationGracePeriodSeconds` expires.
- Components NEVER execute raw `std::process::exit()` before awaiting the `TaskTracker::wait()` completion barrier and flushing pending OpenTelemetry or tracing telemetry spans.

## Bad vs Good Implementations
### Graceful Drain and Task Cancellation Barrier in Axum 0.8

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The HTTP server lacks a graceful shutdown signal, dropping open connections on SIGTERM. Additionally, the spawned background task is detached and untracked; when the process receives a termination signal, it exits immediately, leaving critical background state mutations half-written and corrupting application invariants.
```rust
use axum::{routing::get, Router};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/work", get(|| async {
        // ANTI-PATTERN: Detached unmonitored task drops mid-execution on SIGTERM
        tokio::spawn(async {
            tokio::time::sleep(std::time::Duration::from_secs(5)).await;
            println!("Critical background persistence finished");
        });
        "Accepted"
    }));

    let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
    // ANTI-PATTERN: Hard drop on process signal; in-flight requests abort instantly
    axum::serve(listener, app).await.unwrap();
}
```

#### Production Standard (Correct)
> **Standard**: Axum 0.8 TCP listener shutdown is coordinated with OS signals (`SIGINT`/`SIGTERM`). Detached background work is registered to a `TaskTracker` with nested `CancellationToken` hierarchies. Once the HTTP listener stops accepting requests, the application enforces a deterministic 10-second bounded drain barrier using `tokio::time::timeout` before process exit.
```rust
use axum::{routing::get, Extension, Router};
use std::time::Duration;
use tokio::{net::TcpListener, signal};
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;

#[derive(Clone)]
struct AppContext {
    tracker: TaskTracker,
    token: CancellationToken,
}

async fn shutdown_signal(token: CancellationToken) {
    let ctrl_c = async {
        signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("Failed to install SIGTERM handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
    token.cancel();
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let tracker = TaskTracker::new();
    let token = CancellationToken::new();
    let ctx = AppContext { tracker: tracker.clone(), token: token.clone() };

    let app = Router::new()
        .route("/work", get(|Extension(ctx): Extension<AppContext>| async move {
            let child_token = ctx.token.child_token();
            ctx.tracker.spawn(async move {
                tokio::select! {
                    _ = child_token.cancelled() => {
                        eprintln!("Task gracefully aborted speculative work");
                    }
                    _ = tokio::time::sleep(Duration::from_secs(2)) => {
                        println!("Critical background work completed");
                    }
                }
            });
            "Accepted"
        }))
        .layer(Extension(ctx));

    let listener = TcpListener::bind("0.0.0.0:3000").await?;
    
    // Phase 1: Drain incoming listener connections
    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal(token))
        .await?;

    // Phase 2: Close tracker and fence lingering tasks with bounded deadline
    tracker.close();
    let drain_timeout = Duration::from_secs(10);
    if tokio::time::timeout(drain_timeout, tracker.wait()).await.is_err() {
        eprintln!("Drain deadline exceeded; forceful termination triggered");
    }

    Ok(())
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing

Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing defines an industrial-grade behavioral standard for backend engineering. Guarantees deterministic zero-downtime rolling deployments in Axum 0.8 by coordinating TCP socket shutdown deadlines with hierarchical Tokio task cancellation fences. Eliminates connection drops, half-closed socket leaks, and uncommitted database writes during SIGTERM lifecycle phases. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unbounded Process Exit via Detached Tokio Spawns and Unshielded Axum Shutdown Signals Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnbounded Process Exit via Detached Tokio Spawns and Unshielded Axum Shutdown SignalsCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS bind `axum

Engineers MUST ALWAYS bind `axum::serve(listener, app).with_graceful_shutdown(signal)` to initiate HTTP listener socket drainage upon receiving OS termination signals.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT spawn naked, unmonitored Tokio tasks via `tokio

Engineers MUST NOT spawn naked, unmonitored Tokio tasks via `tokio::spawn` within Axum request handlers or background workers; all spawned tasks MUST be registered to a `tokio_util::task::TaskTracker` or `tokio::task::JoinSet`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST employ hierarchical `tokio_util

Engineers MUST employ hierarchical `tokio_util::sync::CancellationToken` tokens to differentiate between interruptible I/O loops and non-interruptible atomic state mutations.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST gate the final process exit behind a strict two-phase timeout fence (`tokio

Services MUST gate the final process exit behind a strict two-phase timeout fence (`tokio::time::timeout`), guaranteeing that stubborn tasks are aborted before Kubernetes `terminationGracePeriodSeconds` expires.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER execute raw `std

Components NEVER execute raw `std::process::exit()` before awaiting the `TaskTracker::wait()` completion barrier and flushing pending OpenTelemetry or tracing telemetry spans.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Axum 0.8 Tokio Graceful Drain and In-Flight Request Cancellation Fencing critical for AI coding assistants?

Guarantees deterministic zero-downtime rolling deployments in Axum 0.8 by coordinating TCP socket shutdown deadlines with hierarchical Tokio task cancellation fences. Eliminates connection drops, half-closed socket leaks, and uncommitted database writes during SIGTERM lifecycle phases. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unbounded Process Exit via Detached Tokio Spawns and Unshielded Axum Shutdown Signals) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Axum