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 flag | Value / default | Applies to | Source |
|---|---|---|---|
max_blocking_threads | Default 512; must be positive; select a measured limit leaving capacity for filesystem/DNS/std I/O | Current Tokio docs | Builder |
Semaphore::new(permits) | Application-selected positive CPU concurrency; acquire before submission and move permit into closure until it exits | Current Tokio docs | Semaphore |
shutdown_timeout | Remaining application shutdown allowance; no universal duration; waits at most the supplied duration for spawned work to stop | Current Tokio docs | Runtime |
Builder::enable_time() | Enable time driver before using timeout; enable_all() enables I/O and time | Current Tokio docs | Builder, 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) | Cause | Fix | Source |
|---|---|---|---|
| Abort has no effect (symptom) | Blocking closure has already started | Cooperative exit or process isolation if hard termination is required; queued abort may prevent start but is not guaranteed | spawn_blocking |
| Missing current timer can trigger a panic (symptom) | Missing time driver or timer constructed outside runtime context | Enable time/all and construct timer inside async runtime execution | timeout |
| Blocking closure submitted during shutdown usually does not run (reported symptom) | Submission occurs during runtime shutdown | Finish essential cleanup before dropping/shutting down runtime | Aaron1011 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