This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-08-tower-service-draining-zero-downtime-handoff

Axum 0.8 Tower Service Draining & Zero-Downtime Socket Handoff

Enforces deterministic graceful shutdown, Tower service-level connection draining, and socket file-descriptor inheritance in Axum 0.8. Guarantees zero dropped TCP connections, prevents mid-stream HTTP/2 frame truncation, and enables uncorrupted WebSocket/SSE termination during continuous deployment orchestrations. Mandates bounded drain deadlines with cancellation tokens to avoid hung container evictions.

Prohibited Anti-Pattern: Abrupt Process Termination and Unbounded tokio::select Without In-Flight Connection Draining

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Service Draining & Zero-Downtime Socket Handoff - Enforces deterministic graceful shutdown, Tower service-level connection draining, and socket file-descriptor inheritance in Axum 0.8. Guarantees zero dropped TCP connections, prevents mid-stream HTTP/2 frame truncation, and enables uncorrupted WebSocket/SSE termination during continuous deployment orchestrations. Mandates bounded drain deadlines with cancellation tokens to avoid hung container evictions.
globs: src/**/*.rs, Cargo.toml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-08-tower-service-draining-zero-downtime-handoff"
---

# Axum 0.8 Tower Service Draining & Zero-Downtime Socket Handoff

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Abrupt Process Termination and Unbounded tokio::select Without In-Flight Connection Draining

## System Constraints
You are a Principal Rust Systems Engineer enforcing Axum 0.8 Tower Service Draining and Zero-Downtime Socket Handoff invariants. Axum 0.8 relies on `axum::serve` coupled with hyper 1.0 drivers where naive process exits abruptly sever TCP connections, resulting in HTTP 502/504 spikes and RST packets at edge proxies. You must ensure all listening sockets are acquired through socket activation (listenfd) or SO_REUSEPORT, graceful shutdown handles are wired to OS signals with bounded timeouts, and all in-flight streaming connections (SSE, WebSockets, chunked bodies) observe structured cancellation tokens to finish flushes before process termination.

## Strict Guidelines
- Engineers MUST ALWAYS bind listeners using socket activation (`listenfd`) or explicitly configured `SO_REUSEPORT` / dual-stack sockets to allow hot binary replacement without port-binding collision or packet dropping.
- Engineers MUST ALWAYS attach `axum::serve(...).with_graceful_shutdown(...)` using a combined signal trap (`SIGINT`, `SIGTERM`) combined with a `tokio_util::sync::CancellationToken` to notify long-lived streaming tasks.
- Engineers MUST NEVER execute bare `std::process::exit` or drop the Tokio runtime before hyper's connection pools and Tower service middleware completely drain their in-flight pipelines.
- Services MUST enforce a hard bounded draining timeout (e.g., via `tokio::time::timeout`) around the shutdown sequence to guarantee pods do not deadlock orchestrator eviction thresholds before `SIGKILL`.
- Streaming responses (Server-Sent Events, WebSockets, gRPC-web) MUST listen to the global `CancellationToken` to issue terminal protocol-specific frames and flush internal buffers before the hyper connection teardown begins.
- Tower services MUST implement connection tracking or use `tower::ServiceBuilder` layers that do not swallow cancellation futures or introduce deadlock during backpressure states at drain time.

## Bad vs Good Implementations
### Naive Axum Server Termination vs. Industrial Zero-Downtime Socket Handoff & Drain

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The naive server creates an exclusive TCP bind without socket activation or reuse capability, blocking smooth process handoff. It lacks signal handling and graceful drain mechanics: any active HTTP request, WebSocket, or streaming response is dropped immediately when the process is sent SIGTERM during a rolling deployment.
```rust
use axum::{routing::get, Router};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "ok" }));
    
    // ANTI-PATTERN: Binds naive TCP socket without socket inheritance or SO_REUSEPORT.
    // No graceful shutdown: drops all active TCP streams, WebSockets, and in-flight SSE
    // requests immediately upon receiving SIGTERM, producing 502 Bad Gateway at ingress.
    let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

#### Production Standard (Correct)
> **Standard**: Implements socket handoff via `listenfd` to receive pre-bound file descriptors from systemd or deployment managers, avoiding binding conflicts. Installs a unified `SIGINT`/`SIGTERM` handler coupled to a `CancellationToken` that informs streaming endpoints to flush state, and wraps the hyper/Tower draining server with a hard 30-second bounded deadline to prevent eviction deadlocks.
```rust
use axum::{routing::get, Router};
use listenfd::ListenFd;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::signal;
use tokio_util::sync::CancellationToken;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let token = CancellationToken::new();
    let app = Router::new()
        .route("/health", get(|| async { "healthy" }))
        .route("/stream", get({ 
            let token = token.clone();
            move || stream_handler(token)
        }));

    // 1. Zero-downtime socket acquisition (socket activation or fallback)
    let mut listenfd = ListenFd::from_env();
    let listener = match listenfd.take_tcp_listener(0)? {
        Some(std_listener) => {
            std_listener.set_nonblocking(true)?;
            TcpListener::from_std(std_listener)?
        }
        None => TcpListener::bind("0.0.0.0:3000").await?,
    };

    // 2. Coordinated signal listener
    let shutdown_token = token.clone();
    let shutdown_signal = async move {
        let ctrl_c = async { signal::ctrl_c().await.expect("failed to install Ctrl+C") };
        #[cfg(unix)]
        let terminate = async {
            signal::unix::signal(signal::unix::SignalKind::terminate())
                .expect("failed to install SIGTERM")
                .recv()
                .await;
        };
        #[cfg(not(unix))]
        let terminate = std::future::pending::<()>();

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

    // 3. Serve with graceful shutdown and bounded drain window
    let server = axum::serve(listener, app.into_make_service())
        .with_graceful_shutdown(shutdown_signal);

    // Hard bounded deadline for long-running connections before forced drop
    tokio::select! {
        res = server => res?,
        _ = tokio::time::sleep(Duration::from_secs(30)), if token.is_cancelled() => {
            eprintln!("Graceful drain deadline exceeded; forcing server exit");
        }
    }

    Ok(())
}

async fn stream_handler(token: CancellationToken) -> &'static str {
    // In real handler: check token.is_cancelled() to flush frames and close cleanly
    "streaming"
}
```

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 Tower Service Draining & Zero-Downtime Socket Handoff

Axum 0.8 Tower Service Draining & Zero-Downtime Socket Handoff defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic graceful shutdown, Tower service-level connection draining, and socket file-descriptor inheritance in Axum 0.8. Guarantees zero dropped TCP connections, prevents mid-stream HTTP/2 frame truncation, and enables uncorrupted WebSocket/SSE termination during continuous deployment orchestrations. Mandates bounded drain deadlines with cancellation tokens to avoid hung container evictions. 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: Abrupt Process Termination and Unbounded tokio::select Without In-Flight Connection Draining 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 DriftAbrupt Process Termination and Unbounded tokio::select Without In-Flight Connection DrainingCritical
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 listeners using socket activation (`listenfd`) or explicitly configured `SO_REUSEPORT` / dual-stack sockets to allow hot binary replacement without port-binding collision or packet dropping.

Engineers MUST ALWAYS bind listeners using socket activation (`listenfd`) or explicitly configured `SO_REUSEPORT` / dual-stack sockets to allow hot binary replacement without port-binding collision or packet dropping.

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 ALWAYS attach `axum

Engineers MUST ALWAYS attach `axum::serve(...).with_graceful_shutdown(...)` using a combined signal trap (`SIGINT`, `SIGTERM`) combined with a `tokio_util::sync::CancellationToken` to notify long-lived streaming tasks.

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 NEVER execute bare `std

Engineers MUST NEVER execute bare `std::process::exit` or drop the Tokio runtime before hyper's connection pools and Tower service middleware completely drain their in-flight pipelines.

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 enforce a hard bounded draining timeout (e.g., via `tokio

Services MUST enforce a hard bounded draining timeout (e.g., via `tokio::time::timeout`) around the shutdown sequence to guarantee pods do not deadlock orchestrator eviction thresholds before `SIGKILL`.

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

Streaming responses (Server-Sent Events, WebSockets, gRPC-web) MUST listen to the global `CancellationToken` to issue terminal protocol-specific frames and flush internal buffers before the hyper connection teardown begins.

Streaming responses (Server-Sent Events, WebSockets, gRPC-web) MUST listen to the global `CancellationToken` to issue terminal protocol-specific frames and flush internal buffers before the hyper connection teardown begins.

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.
6

Tower services MUST implement connection tracking or use `tower

Tower services MUST implement connection tracking or use `tower::ServiceBuilder` layers that do not swallow cancellation futures or introduce deadlock during backpressure states at drain time.

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 Tower Service Draining & Zero-Downtime Socket Handoff critical for AI coding assistants?

Enforces deterministic graceful shutdown, Tower service-level connection draining, and socket file-descriptor inheritance in Axum 0.8. Guarantees zero dropped TCP connections, prevents mid-stream HTTP/2 frame truncation, and enables uncorrupted WebSocket/SSE termination during continuous deployment orchestrations. Mandates bounded drain deadlines with cancellation tokens to avoid hung container evictions. 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 (Abrupt Process Termination and Unbounded tokio::select Without In-Flight Connection Draining) 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