This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-tokio-cooperative-budget-partitioning

Axum 0.8 Tokio Cooperative Task-Budget Partitioning Invariant

Enforces explicit cooperative budget exhaustion fences via tokio::task::consume_budget inside high-throughput Axum 0.8 middleware, extractors, and request transform pipelines. Prevents asymmetric multi-threaded runtime worker starvation and tail latency spikes caused by CPU-bound or unbudgeted compute iterations executed directly on the Tokio async reactor loop.

Prohibited Anti-Pattern: Unbudgeted In-Reactor Synchronous Compute Starving Tokio Worker Threads

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tokio Cooperative Task-Budget Partitioning Invariant - Enforces explicit cooperative budget exhaustion fences via tokio::task::consume_budget inside high-throughput Axum 0.8 middleware, extractors, and request transform pipelines. Prevents asymmetric multi-threaded runtime worker starvation and tail latency spikes caused by CPU-bound or unbudgeted compute iterations executed directly on the Tokio async reactor loop.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tokio-cooperative-budget-partitioning"
---

# Axum 0.8 Tokio Cooperative Task-Budget Partitioning Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbudgeted In-Reactor Synchronous Compute Starving Tokio Worker Threads

## System Constraints
You are a Principal Rust Distributed Systems Architect enforcing Axum 0.8 and Tokio runtime safety invariants. Tokio's cooperative scheduler defaults to a 128-tick operational budget per task, but purely computational loops, decoding passes, cryptographic verifications, and parsing steps within Axum middleware do not automatically decrement or check the cooperative scheduler budget unless explicit yield boundaries are set. You must enforce explicit `tokio::task::consume_budget().await` fences or `tokio::task::spawn_blocking` offloading to prevent asymmetric worker starvation.

## Strict Guidelines
- Engineers MUST ALWAYS insert `tokio::task::consume_budget().await` within non-trivial iterations (e.g., chunk validation, batch claims verification, or header parsing) inside Axum middleware before invoking downstream handlers.
- Engineers MUST NOT execute blocking or CPU-intensive computations exceeding 10–50 microseconds directly in Axum 0.8 `from_fn` middleware or custom extractors without partitioning via `consume_budget` or `tokio::task::spawn_blocking`.
- Services MUST prefer `tokio::task::consume_budget().await` over unconditional `tokio::task::yield_now().await` in hot path middleware to avoid redundant context switching when task budget remains available.
- Code SHOULD bound streaming body consumption and apply cooperative yielding between stream chunk decodes to guarantee fair scheduling across all concurrent Axum connections.
- Components NEVER hold synchronous standard library locks (`std::sync::Mutex`) across cooperative yield points or downstream service calls in Axum pipelines.

## Bad vs Good Implementations
### Axum 0.8 Streaming Header and Token Processing Middleware

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Iterating across large sets of request headers or complex signature chains directly on the Tokio async task without cooperative budget consumption monopolizes the OS thread. Tokio's automatic budget only decrements on Tokio I/O or timer operations, allowing CPU bursts to starve neighboring tasks on the same worker queue.
```rust
use axum::{
    extract::Request,
    middleware::Next,
    response::Response,
    http::StatusCode,
};

pub async fn verify_token_chain(req: Request, next: Next) -> Result<Response, StatusCode> {
    let headers = req.headers();
    
    // ANTI-PATTERN: Heavy iteration/parsing runs synchronously on Tokio worker.
    // Bypasses cooperative scheduling; other tasks multiplexed on this worker thread
    // suffer severe tail latency and worker starvation.
    for (key, val) in headers.iter() {
        let val_bytes = val.as_bytes();
        for chunk in val_bytes.chunks(64) {
            let _ = ring::digest::digest(&ring::digest::SHA256, chunk);
        }
    }

    Ok(next.run(req).await)
}
```

#### Production Standard (Correct)
> **Standard**: Invoking `tokio::task::consume_budget().await` participates in Tokio's cooperative multitasking contract. It decrements the scheduler's per-turn iteration budget and yields control back to the worker's run queue only when exhausted, eliminating thread starvation while avoiding the overhead of unconditional yields.
```rust
use axum::{
    extract::Request,
    middleware::Next,
    response::Response,
    http::StatusCode,
};

pub async fn verify_token_chain(req: Request, next: Next) -> Result<Response, StatusCode> {
    let headers = req.headers();
    
    for (key, val) in headers.iter() {
        let val_bytes = val.as_bytes();
        for chunk in val_bytes.chunks(64) {
            // INVARIANT: Check and decrement Tokio cooperative budget.
            // Yields back to the reactor only if the task's budget quota is exhausted.
            tokio::task::consume_budget().await;
            
            let _ = ring::digest::digest(&ring::digest::SHA256, chunk);
        }
    }

    Ok(next.run(req).await)
}
```

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 Cooperative Task-Budget Partitioning Invariant

Axum 0.8 Tokio Cooperative Task-Budget Partitioning Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces explicit cooperative budget exhaustion fences via tokio::task::consume_budget inside high-throughput Axum 0.8 middleware, extractors, and request transform pipelines. Prevents asymmetric multi-threaded runtime worker starvation and tail latency spikes caused by CPU-bound or unbudgeted compute iterations executed directly on the Tokio async reactor loop. 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: Unbudgeted In-Reactor Synchronous Compute Starving Tokio Worker Threads 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 DriftUnbudgeted In-Reactor Synchronous Compute Starving Tokio Worker ThreadsCritical
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 insert `tokio

Engineers MUST ALWAYS insert `tokio::task::consume_budget().await` within non-trivial iterations (e.g., chunk validation, batch claims verification, or header parsing) inside Axum middleware before invoking downstream handlers.

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 execute blocking or CPU-intensive computations exceeding 10–50 microseconds directly in Axum 0.8 `from_fn` middleware or custom extractors without partitioning via `consume_budget` or `tokio

Engineers MUST NOT execute blocking or CPU-intensive computations exceeding 10–50 microseconds directly in Axum 0.8 `from_fn` middleware or custom extractors without partitioning via `consume_budget` or `tokio::task::spawn_blocking`.

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

Services MUST prefer `tokio::task::consume_budget().await` over unconditional `tokio::task::yield_now().await` in hot path middleware to avoid redundant context switching when task budget remains available.

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

Code SHOULD bound streaming body consumption and apply cooperative yielding between stream chunk decodes to guarantee fair scheduling across all concurrent Axum connections.

Code SHOULD bound streaming body consumption and apply cooperative yielding between stream chunk decodes to guarantee fair scheduling across all concurrent Axum connections.

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 hold synchronous standard library locks (`std

Components NEVER hold synchronous standard library locks (`std::sync::Mutex`) across cooperative yield points or downstream service calls in Axum 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.
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 Cooperative Task-Budget Partitioning Invariant critical for AI coding assistants?

Enforces explicit cooperative budget exhaustion fences via tokio::task::consume_budget inside high-throughput Axum 0.8 middleware, extractors, and request transform pipelines. Prevents asymmetric multi-threaded runtime worker starvation and tail latency spikes caused by CPU-bound or unbudgeted compute iterations executed directly on the Tokio async reactor loop. 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 (Unbudgeted In-Reactor Synchronous Compute Starving Tokio Worker Threads) 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