Skip to content

No capture from std::thread::spawn or tokio::task::spawn_blocking #23

@elonen

Description

@elonen

I'm having difficulties asserting traces from inside an std thread. Is there something I overlooked, or does the capture perhaps generally fail when using std::thread::spawn instead of multi-threading tokio?

The following passes first two tests but fails the third:

use tracing;
use tracing_test::traced_test;

fn hello() {
    tracing::info!("Hello, world!");
}

#[test]
#[traced_test]
fn test_1_singlethread()
{
    tracing::info!("Test thread");
    hello();
    assert!(logs_contain("Test thread"));
    assert!(logs_contain("Hello, world!"));
}

#[tokio::test]
#[traced_test]
async fn test_2_multithread_tokio()
{
    tracing::info!("Test thread");
    tokio::spawn(async { hello(); }).await.unwrap();
    assert!(logs_contain("Test thread"));
    assert!(logs_contain("Hello, world!"));
}

#[test]
#[traced_test]
fn test_3_multithread_std()
{
    tracing::info!("Test thread");
    std::thread::spawn(|| { hello(); }).join().unwrap();
    assert!(logs_contain("Test thread"));
    assert!(logs_contain("Hello, world!"));
}

Results:

[...]
running 3 tests
test test_1_singlethread ... ok
test test_2_multithread_tokio ... ok
test test_3_multithread_std ... FAILED

failures:

---- test_3_multithread_std stdout ----
2023-02-12T08:02:20.047401Z  INFO test_3_multithread_std: r#mod: Test thread
2023-02-12T08:02:20.047625Z  INFO r#mod: Hello, world!
thread 'test_3_multithread_std' panicked at 'assertion failed: logs_contain(\"Hello, world!\")', tests/mod.rs:35:5

Cargo.toml contains no-env-filter:

[package]
name = "tracing-test-problem"
version = "0.1.0"
edition = "2021"

[dependencies]
tracing = "0.1.37"
tokio = { version = "1.25.0", features = ["rt-multi-thread", "macros"] }

[dev-dependencies]
tracing-test = {version="0.2.4", features=["no-env-filter"] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions