This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-0-8-cancellation-and-socket-draining

Axum 0.8 Tower Service Graceful Cancellation and Socket Draining

Enforces deterministic upstream socket draining, cancellation safety, and connection lifecycle management in Axum 0.8 and Hyper 1.x. Prevents TCP socket descriptor leaks, buffer starvation, and half-open connection accumulation during high-concurrency client disconnect storms.

Prohibited Anti-Pattern: Unbounded Body Drops and Non-Cancellation-Safe Select Loops Causing Socket Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower Service Graceful Cancellation and Socket Draining - Enforces deterministic upstream socket draining, cancellation safety, and connection lifecycle management in Axum 0.8 and Hyper 1.x. Prevents TCP socket descriptor leaks, buffer starvation, and half-open connection accumulation during high-concurrency client disconnect storms.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-0-8-cancellation-and-socket-draining"
---

# Axum 0.8 Tower Service Graceful Cancellation and Socket Draining

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Body Drops and Non-Cancellation-Safe Select Loops Causing Socket Starvation

## System Constraints
You are a Principal Systems Architect enforcing strict invariants around Axum 0.8, Tower 0.5, and Hyper 1.x socket lifecycle management. You mandate explicit body consumption/draining on cancelled streams, cancellation-safe future composition in `tokio::select!`, and deterministic connection cleanup via structured shutdown primitives to prevent socket descriptor exhaustion during disconnect bursts.

## Strict Guidelines
- Engineers MUST ALWAYS ensure handlers reading streaming request bodies (`axum::body::Body`) drain or explicitly drop the frame stream when processing fails, preventing Hyper from holding socket buffers open for unconsumed data.
- Engineers MUST NOT drop asynchronous request body streams inside non-cancellation-safe `tokio::select!` branches without handling partial frame state and connection teardown.
- Services MUST utilize `axum::serve(listener, app).with_graceful_shutdown(...)` in conjunction with `tokio_util::sync::CancellationToken` to orchestrate zero-downtime draining of inflight TCP sockets.
- Components NEVER spawn unmanaged background tasks holding request bodies or response streaming channels; all delegated execution must be tied to a parent cancellation token or structured task tracker (`tokio_util::task::TaskTracker`).
- Code SHOULD configure explicit TCP keepalive, connect, and read timeouts on underlying listeners using `socket2` or `tokio::net::TcpListener` options before passing to `axum::serve` to prevent lingering zombie sockets in CLOSE_WAIT/FIN_WAIT states.

## Bad vs Good Implementations
### Handling Streaming Request Body Cancellation and Graceful Draining

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Abruptly aborting an in-flight streaming read inside a `tokio::select!` without consuming or aborting the underlying body leaves incoming frames unread. The hyper connection worker must buffer incoming TCP chunks or wait until transport-level reset, causing file descriptor starvation during disconnect bursts.
```rust
use axum::{body::Body, extract::Request, response::Response, http::StatusCode};
use tokio::time::{sleep, Duration};

pub async fn process_upload(req: Request<Body>) -> Result<Response, StatusCode> {
    let mut body = req.into_body();
    tokio::select! {
        // ANTI-PATTERN: If the timeout fires, the body reading future is abruptly dropped.
        // In Hyper 1.0, unconsumed body chunks leave TCP socket buffers undrained,
        // causing connection pool starvation and delayed socket recycling under load.
        _ = sleep(Duration::from_secs(5)) => {
            Err(StatusCode::REQUEST_TIMEOUT)
        }
        res = async {
            // Naive read loop without cancellation draining
            while let Some(chunk) = http_body_util::BodyExt::frame(&mut body).await {
                let _ = chunk.map_err(|_| StatusCode::BAD_REQUEST)?;
            }
            Ok(StatusCode::OK)
        } => {
            res.map(|code| Response::builder().status(code).body(Body::empty()).unwrap())
        }
    }
}
```

#### Production Standard (Correct)
> **Standard**: The good example cleanly isolates the stream reader, handles cancellation in a biased select branch, and ensures deterministic teardown when timeouts occur, allowing Hyper 1.x to emit connection resets or close frames without socket buffer buildup.
```rust
use axum::{body::Body, extract::Request, response::Response, http::StatusCode};
use futures_util::StreamExt;
use http_body_util::BodyStream;
use tokio::time::{timeout, Duration};
use tokio_util::sync::CancellationToken;

pub async fn process_upload(
    token: CancellationToken,
    req: Request<Body>,
) -> Result<Response, StatusCode> {
    let (parts, body) = req.into_parts();
    let mut stream = BodyStream::new(body);

    let processing_future = async {
        while let Some(frame_res) = stream.next().await {
            let frame = frame_res.map_err(|_| StatusCode::BAD_REQUEST)?;
            if frame.is_data() {
                // Deterministic processing per frame
            }
        }
        Ok::<_, StatusCode>(())
    };

    // Invariant: Enforce explicit bounded execution with cooperative token checking
    tokio::select! {
        biased;
        _ = token.cancelled() => {
            // Upstream connection canceled; explicit early return drops stream cleanly
            Err(StatusCode::SERVICE_UNAVAILABLE)
        }
        res = timeout(Duration::from_secs(5), processing_future) => {
            match res {
                Ok(Ok(())) => Ok(Response::builder().status(StatusCode::OK).body(Body::empty()).unwrap()),
                Ok(Err(status)) => Err(status),
                Err(_) => {
                    // Explicitly terminate with 408; stream is deterministically dropped here
                    Err(StatusCode::REQUEST_TIMEOUT)
                }
            }
        }
    }
}
```

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 Graceful Cancellation and Socket Draining

Axum 0.8 Tower Service Graceful Cancellation and Socket Draining defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic upstream socket draining, cancellation safety, and connection lifecycle management in Axum 0.8 and Hyper 1.x. Prevents TCP socket descriptor leaks, buffer starvation, and half-open connection accumulation during high-concurrency client disconnect storms. 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 Body Drops and Non-Cancellation-Safe Select Loops Causing Socket Starvation 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 Body Drops and Non-Cancellation-Safe Select Loops Causing Socket StarvationCritical
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 ensure handlers reading streaming request bodies (`axum

Engineers MUST ALWAYS ensure handlers reading streaming request bodies (`axum::body::Body`) drain or explicitly drop the frame stream when processing fails, preventing Hyper from holding socket buffers open for unconsumed data.

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 drop asynchronous request body streams inside non-cancellation-safe `tokio

Engineers MUST NOT drop asynchronous request body streams inside non-cancellation-safe `tokio::select!` branches without handling partial frame state and connection teardown.

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

Services MUST utilize `axum

Services MUST utilize `axum::serve(listener, app).with_graceful_shutdown(...)` in conjunction with `tokio_util::sync::CancellationToken` to orchestrate zero-downtime draining of inflight TCP sockets.

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

Components NEVER spawn unmanaged background tasks holding request bodies or response streaming channels

Components NEVER spawn unmanaged background tasks holding request bodies or response streaming channels; all delegated execution must be tied to a parent cancellation token or structured task tracker (`tokio_util::task::TaskTracker`).

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

Code SHOULD configure explicit TCP keepalive, connect, and read timeouts on underlying listeners using `socket2` or `tokio

Code SHOULD configure explicit TCP keepalive, connect, and read timeouts on underlying listeners using `socket2` or `tokio::net::TcpListener` options before passing to `axum::serve` to prevent lingering zombie sockets in CLOSE_WAIT/FIN_WAIT states.

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 Graceful Cancellation and Socket Draining critical for AI coding assistants?

Enforces deterministic upstream socket draining, cancellation safety, and connection lifecycle management in Axum 0.8 and Hyper 1.x. Prevents TCP socket descriptor leaks, buffer starvation, and half-open connection accumulation during high-concurrency client disconnect storms. 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 Body Drops and Non-Cancellation-Safe Select Loops Causing Socket Starvation) 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