This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-streaming-cancellation-barrier

Axum 0.8 Streaming Extractor Task Cancellation Barrier

Mandates deterministic cancellation boundaries for all Axum 0.8 streaming responses, SSE, and WebSocket loops. When hyper drops an active response stream upon client disconnect, detached background worker tasks must be proactively terminated via CancellationToken or RAII drop guards to prevent unbounded memory and thread leaks.

Prohibited Anti-Pattern: Spawning Detached Background Producers Without Cancellation Barriers in Streaming Handlers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Streaming Extractor Task Cancellation Barrier - Mandates deterministic cancellation boundaries for all Axum 0.8 streaming responses, SSE, and WebSocket loops. When hyper drops an active response stream upon client disconnect, detached background worker tasks must be proactively terminated via CancellationToken or RAII drop guards to prevent unbounded memory and thread leaks.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-streaming-cancellation-barrier"
---

# Axum 0.8 Streaming Extractor Task Cancellation Barrier

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Spawning Detached Background Producers Without Cancellation Barriers in Streaming Handlers

## System Constraints
You are a Principal Rust Concurrency Architect enforcing Axum 0.8 streaming safety. Abrupt client disconnects drop hyper response streams; you must guarantee no background task continues executing orphaned asynchronous workloads, holding database connections, or exhausting thread pools.

## Strict Guidelines
- Engineers MUST ALWAYS couple background stream producers to a `tokio_util::sync::CancellationToken` or an RAII `AbortHandle` drop guard.
- Engineers MUST NOT spawn detached `tokio::spawn` loops inside SSE or WebSocket handlers without a reciprocal client termination listener.
- Code SHOULD prefer `async_stream::try_stream!` or `futures::stream::unfold` where stream cancellation natively drops the generator future without manual task synchronization.
- Services MUST ensure that channels transmitting streaming chunks to Axum response adapters yield early exits when `Sender::send` or `Sender::reserve` returns `SendError`.
- Components NEVER rely on TCP keep-alive or slow-disconnect timeouts to cleanup leaked generator tasks.

## Bad vs Good Implementations
### SSE Response Task Leak vs RAII Cancellation Guard

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Hyper drops `ReceiverStream` when the client terminates the connection, but `state.expensive_database_cursor().await` blocks independently inside a detached tokio task. The worker leaks active DB handles, CPU cycles, and memory until completion.
```rust
use axum::{extract::State, response::sse::{Event, Sse}};
use futures::stream::Stream;
use std::convert::Infallible;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;

pub async fn handler_bad(State(state): State<AppState>) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let (tx, rx) = mpsc::channel(32);
    
    // ANTI-PATTERN: Detached task spawned with no cancellation token.
    // If client disconnects immediately, expensive DB queries and remote LLM tokens
    // continue executing until the buffer fills and send fails, or indefinitely if blocked on I/O.
    tokio::spawn(async move {
        while let Some(item) = state.expensive_database_cursor().await {
            let _ = tx.send(Ok(Event::default().data(item))).await;
        }
    });

    Sse::new(ReceiverStream::new(rx))
}
```

#### Production Standard (Correct)
> **Standard**: Using a `DropGuard` from `tokio_util::sync::CancellationToken` guarantees that hyper dropping the SSE stream immediately cancels the child task, instantly aborting ongoing database I/O and freeing resources.
```rust
use axum::{extract::State, response::sse::{Event, Sse}};
use futures::stream::Stream;
use std::convert::Infallible;
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;
use tokio::sync::mpsc;

pub async fn handler_good(State(state): State<AppState>) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let token = CancellationToken::new();
    let child_token = token.child_token();
    let (tx, rx) = mpsc::channel(32);

    tokio::spawn(async move {
        tokio::select! {
            _ = child_token.cancelled() => {
                // Deterministic cancellation barrier triggered on client disconnect
            }
            _ = async {
                while let Some(item) = state.expensive_database_cursor().await {
                    if tx.send(Ok(Event::default().data(item))).await.is_err() {
                        break;
                    }
                }
            } => {}
        }
    });

    // Guard automatically triggers child_token.cancel() when Axum drops the stream
    let stream = ReceiverStream::new(rx);
    let guarded_stream = async_stream::stream! {
        let _guard = token.drop_guard();
        for await item in stream {
            yield item;
        }
    };

    Sse::new(guarded_stream)
}
```

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 Streaming Extractor Task Cancellation Barrier

Axum 0.8 Streaming Extractor Task Cancellation Barrier defines an industrial-grade behavioral standard for backend engineering. Mandates deterministic cancellation boundaries for all Axum 0.8 streaming responses, SSE, and WebSocket loops. When hyper drops an active response stream upon client disconnect, detached background worker tasks must be proactively terminated via CancellationToken or RAII drop guards to prevent unbounded memory and thread leaks. 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: Spawning Detached Background Producers Without Cancellation Barriers in Streaming Handlers 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 DriftSpawning Detached Background Producers Without Cancellation Barriers in Streaming HandlersCritical
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 couple background stream producers to a `tokio_util

Engineers MUST ALWAYS couple background stream producers to a `tokio_util::sync::CancellationToken` or an RAII `AbortHandle` drop guard.

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 detached `tokio

Engineers MUST NOT spawn detached `tokio::spawn` loops inside SSE or WebSocket handlers without a reciprocal client termination listener.

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

Code SHOULD prefer `async_stream

Code SHOULD prefer `async_stream::try_stream!` or `futures::stream::unfold` where stream cancellation natively drops the generator future without manual task synchronization.

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 ensure that channels transmitting streaming chunks to Axum response adapters yield early exits when `Sender

Services MUST ensure that channels transmitting streaming chunks to Axum response adapters yield early exits when `Sender::send` or `Sender::reserve` returns `SendError`.

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 rely on TCP keep-alive or slow-disconnect timeouts to cleanup leaked generator tasks.

Components NEVER rely on TCP keep-alive or slow-disconnect timeouts to cleanup leaked generator 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.
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 Streaming Extractor Task Cancellation Barrier critical for AI coding assistants?

Mandates deterministic cancellation boundaries for all Axum 0.8 streaming responses, SSE, and WebSocket loops. When hyper drops an active response stream upon client disconnect, detached background worker tasks must be proactively terminated via CancellationToken or RAII drop guards to prevent unbounded memory and thread leaks. 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 (Spawning Detached Background Producers Without Cancellation Barriers in Streaming Handlers) 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