CodeSampleX

Sample

tokio 1.43.0: Calling blocking operations and nested block_on inside Tokio contexts requires navigating runtime-flavor panic traps and thread affinity guarantees

Verified sample for cargo tokio 1.43.0: Calling blocking operations and nested block_on inside Tokio contexts requires navigating runtime-flavor panic traps…

sha256:df2946c399f0c7e7df2da45bd620bd315d8fd33f401883465c71605cfba3bc53

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
3
Signing keys that built it
3
Declared environment rust linux x64 rust rust cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:ac1544ece1e22594 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-15
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Case

HOW
Goal
Calling blocking operations and nested block_on inside Tokio contexts requires navigating runtime-flavor panic traps and thread affinity guarantees
Packages
Symbols
  • tokio::runtime::Handle::block_on
  • tokio::task::block_in_place
  • tokio::task::spawn_blocking
Environment
rust
Created
2026-08-15T02:51:06Z

Contract

  1. assert Handle::current().block_on panics when invoked directly inside an async worker thread
  2. assert tokio::task::block_in_place panics on current_thread runtime flavor even if rt-multi-thread feature is enabled
  3. assert tokio::task::block_in_place executes on the exact same OS thread and enables nested Handle::current().block_on without panicking
  4. assert tokio::task::spawn_blocking switches to a distinct OS thread on current_thread and multi_thread runtimes
  5. assert tokio::task::spawn_blocking threads allow direct Handle::current().block_on calls without block_in_place

Files

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

Download the source artifact (tar.gz)

Source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
 "gimli",
]

[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"

[[package]]
name = "backtrace"
version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
dependencies = [
 "addr2line",
 "cfg-if",
 "libc",
 "miniz_oxide",
 "object",
 "rustc-demangle",
 "windows-link",
]

[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"

[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"

[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"

[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
 "adler2",
]

[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
dependencies = [
 "memchr",
]

[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "rustc-demangle"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb"

[[package]]
name = "sample"
version = "0.1.0"
dependencies = [
 "tokio",
]

[[package]]
name = "syn"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "tokio"
version = "1.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
dependencies = [
 "backtrace",
 "pin-project-lite",
 "tokio-macros",
]

[[package]]
name = "tokio-macros"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
Cargo.toml
[package]
name = "sample"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "=1.43.0", features = ["rt", "rt-multi-thread", "macros"] }
NOTES.md
What a model would have written instead: Models typically believe `tokio::task::block_in_place` is a universal drop-in for blocking code in any Tokio runtime and that calling `Handle::current().block_on` from within any runtime context always panics with "Cannot start a runtime from within a runtime".
How the wrong version fails: Calling `block_in_place` on a `current_thread` runtime (or single-threaded flavor) fails loudly with a runtime panic ("can call blocking only when running on the multi-threaded runtime"), while assuming `Handle::current().block_on` is forbidden inside `spawn_blocking` or `block_in_place` causes models to introduce complex async bridge channels or thread-hopping that silently obscures thread-local bindings and incurs synchronization overhead.
csx.json
{"case":{"caseId":"case:sha256:59a27c191c041c6122dcd125b8f8c819acfda39d5024d46a8cdccce8b0bb6759","constraints":{"runtime":"rust"},"contract":["assert Handle::current().block_on panics when invoked directly inside an async worker thread","assert tokio::task::block_in_place panics on current_thread runtime flavor even if rt-multi-thread feature is enabled","assert tokio::task::block_in_place executes on the exact same OS thread and enables nested Handle::current().block_on without panicking","assert tokio::task::spawn_blocking switches to a distinct OS thread on current_thread and multi_thread runtimes","assert tokio::task::spawn_blocking threads allow direct Handle::current().block_on calls without block_in_place"],"goal":"Calling blocking operations and nested block_on inside Tokio contexts requires navigating runtime-flavor panic traps and thread affinity guarantees","kind":"HOW","packages":["pkg:cargo/tokio@1.43.0"],"schemaVersion":1,"symbols":["tokio::runtime::Handle::block_on","tokio::task::block_in_place","tokio::task::spawn_blocking"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/tokio@1.43.0"],"schemaVersion":1,"symbols":["tokio::runtime::Handle::block_on","tokio::task::block_in_place","tokio::task::spawn_blocking"],"verifierAdapter":"cargo@1"}
src/lib.rs
use std::panic::catch_unwind;
use std::thread;

/// 1. Direct Handle::block_on inside an async worker thread panics
pub fn direct_block_on_in_worker_panics() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .build()
        .unwrap();

    rt.block_on(async {
        let result = catch_unwind(|| {
            tokio::runtime::Handle::current().block_on(async { 42 })
        });
        assert!(result.is_err(), "Direct Handle::block_on on worker thread must panic");
    });
}

/// 2. block_in_place panics on current_thread runtime even with rt-multi-thread compiled in
pub fn block_in_place_panics_on_current_thread() {
    let rt = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();

    rt.block_on(async {
        let result = catch_unwind(|| {
            tokio::task::block_in_place(|| 42)
        });
        assert!(result.is_err(), "block_in_place on current_thread runtime must panic");
    });
}

/// 3. block_in_place on multi_thread runtime runs on the SAME thread and allows nested block_on
pub fn block_in_place_runs_on_same_thread_and_allows_block_on() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .build()
        .unwrap();

    rt.block_on(async {
        let async_thread_id = thread::current().id();

        let (blocking_thread_id, nested_val) = tokio::task::block_in_place(|| {
            let b_id = thread::current().id();
            let val = tokio::runtime::Handle::current().block_on(async { 42 });
            (b_id, val)
        });

        assert_eq!(async_thread_id, blocking_thread_id, "block_in_place executes on the SAME OS thread");
        assert_eq!(nested_val, 42, "block_in_place allows nested block_on");
    });
}

/// 4. spawn_blocking works on current_thread runtime and runs on a DIFFERENT thread
pub fn spawn_blocking_works_on_current_thread_and_switches_thread() {
    let rt = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();

    rt.block_on(async {
        let async_thread_id = thread::current().id();

        let blocking_thread_id = tokio::task::spawn_blocking(move || {
            thread::current().id()
        }).await.unwrap();

        assert_ne!(async_thread_id, blocking_thread_id, "spawn_blocking runs on a distinct thread even on current_thread runtime");
    });
}

/// 5. spawn_blocking thread allows Handle::block_on without block_in_place
pub fn spawn_blocking_allows_nested_block_on() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .build()
        .unwrap();

    rt.block_on(async {
        let val = tokio::task::spawn_blocking(|| {
            // No block_in_place needed inside spawn_blocking thread
            tokio::runtime::Handle::current().block_on(async { 99 })
        }).await.unwrap();

        assert_eq!(val, 99, "spawn_blocking thread can run Handle::block_on directly");
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_direct_block_on_in_worker_panics() {
        direct_block_on_in_worker_panics();
    }

    #[test]
    fn test_block_in_place_panics_on_current_thread() {
        block_in_place_panics_on_current_thread();
    }

    #[test]
    fn test_block_in_place_runs_on_same_thread_and_allows_block_on() {
        block_in_place_runs_on_same_thread_and_allows_block_on();
    }

    #[test]
    fn test_spawn_blocking_works_on_current_thread_and_switches_thread() {
        spawn_blocking_works_on_current_thread_and_switches_thread();
    }

    #[test]
    fn test_spawn_blocking_allows_nested_block_on() {
        spawn_blocking_allows_nested_block_on();
    }
}

Origin Seeder

anonymous