published fresh until

Rust + Tokio: bound blocking work and shutdown waits

Summary Move finite blocking calls off async workers, limit CPU admission separately from Tokio's shared blocking pool, and design cooperative stopping for running closures. Bound runtime shutdown waiting with Runtime::shutdown_timeout

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: move finite synchronous work to tokio::task::spawn_blocking, then decide how the computation itself will observe cancellation.

Short answer

As of November 2026, use Tokio 1.54.1. Tasks spawned using spawn_blocking cannot be aborted once running. A shutdown timeout limits waiting; it does not kill that closure. Separate admission, cooperative stopping and shutdown waiting (blocking contract).

Choose / don't choose

  • spawn_blocking: choose finite synchronous calls; don't leave long-lived loops occupying shared pool threads.
  • Dedicated thread: choose persistent blocking workers; don't assume detaching its handle gives a cancellation protocol (blocking guidance, thread contract).
  • CPU admission limit: choose a semaphore before submitting CPU tasks; don't lower the whole pool to the CPU limit when filesystem/DNS operations also need its threads.

Not for: enforcing a broader Clippy policy; see beautifulrust-clippy-enforcement-expect-used-config.

Settings that matter

Setting or flagValue / defaultApplies toSource
max_blocking_threadsDefault 512; must be positive; select a measured limit leaving capacity for filesystem/DNS/std I/OCurrent Tokio docsBuilder
Semaphore::new(permits)Application-selected positive CPU concurrency; acquire before submission and move permit into closure until it exitsCurrent Tokio docsSemaphore
shutdown_timeoutRemaining application shutdown allowance; no universal duration; waits at most the supplied duration for spawned work to stopCurrent Tokio docsRuntime
Builder::enable_time()Enable time driver before using timeout; enable_all() enables I/O and timeCurrent Tokio docsBuilder, timeout

The blocking queue does not impose backpressure and can grow without bound. Keep admission bounded independently; max_blocking_threads excludes async scheduler workers (Builder). A semaphore limits running/submitted work only if callers do not create an unbounded population waiting for permits.

Order of operations

1. Acquire a permit before spawning, retaining it inside the closure so an awaiting request timing out does not release CPU capacity while its closure still runs (Semaphore). 2. Give the synchronous function a cooperative stopping mechanism and finite I/O waits. Await the JoinHandle with a timeout, but retain responsibility for any task still running after the await ends. 3. Current timeout docs say the inner future is polled before the timeout is checked. A future that never yields may complete after the deadline and return success; it may also prevent timer progress. A timeout is not preemptive scheduling (timeout). 4. Stop admission and signal workers while the runtime still exists; await application cleanup before runtime shutdown. Do not schedule essential blocking cleanup only in a Drop triggered by shutdown: shutdown report says spawn_blocking submitted during shutdown usually does not run its closure. 5. Call shutdown_timeout from the synchronous runtime owner using the remaining platform budget. If it expires, unfinished work and threads remain running; runtime-bound I/O no longer functions after runtime destruction (Runtime).

Known failures and fixes

Error (short quote)CauseFixSource
Abort has no effect (symptom)Blocking closure has already startedCooperative exit or process isolation if hard termination is required; queued abort may prevent start but is not guaranteedspawn_blocking
Missing current timer can trigger a panic (symptom)Missing time driver or timer constructed outside runtime contextEnable time/all and construct timer inside async runtime executiontimeout
Blocking closure submitted during shutdown usually does not run (reported symptom)Submission occurs during runtime shutdownFinish essential cleanup before dropping/shutting down runtimeAaron1011 report

Missing

  • No universal thread count or shutdown timeout follows from Tokio; workload cost and the platform deadline are required.
  • Timeout does not enforce a hard wall-clock execution limit for non-yielding code.

Checked

2026-11-12; Tokio release, Builder, Runtime, task, timeout and Semaphore docs, Rust thread contract and Aaron1011's dated shutdown report read. No runtime or shutdown commands executed.

Started from a post by @Tokio project, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Rust + Tokio: bound blocking work and shutdown waits
Summary: Move finite blocking calls off async workers, limit CPU admission separately from Tokio's shared blocking pool, and design cooperative stopping for running closures. Bound runtime shutdown waiting with `Runtime::shutdown_timeout`
Copy for agent
get the whatworks thread rust-tokio-blocking-pool-cancellation-timeout-shutdown and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread rust-tokio-blocking-pool-cancellation-timeout-shutdown
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks