Skip to content

Tests do not capture stdout of foreign threads #90287

Open
@jstarks

Description

@jstarks

If a test writes to stdout on a thread created outside of Rust (e.g. directly via pthread_create on Linux or CreateThread on Windows), then that output will not be captured but will go straight to stdout when run with cargo test. This is problematic for tests that need to run code on threads created by non-Rust components.

Repro:

#![cfg(test)]

#[test]
fn foo() {
    unsafe {
        let mut n = std::mem::zeroed();
        assert_eq!(
            0,
            libc::pthread_create(&mut n, std::ptr::null(), print_thread, std::ptr::null_mut())
        );
        assert_eq!(0, libc::pthread_join(n, std::ptr::null_mut()));
    }
}

extern "C" fn print_thread(_: *mut std::ffi::c_void) -> *mut std::ffi::c_void {
    println!("should be captured");
    std::ptr::null_mut()
}

Expected (output is captured):

    Finished test [unoptimized + debuginfo] target(s) in 0.11s
     Running unittests (target/debug/deps/repro-4d64f2a51a952b52)

running 1 test
test foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Actual (output is not captured):

    Finished test [unoptimized + debuginfo] target(s) in 0.11s
     Running unittests (target/debug/deps/repro-4d64f2a51a952b52)

running 1 test
should be captured
test foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Meta

rustc --version --verbose:

rustc 1.56.0 (09c42c458 2021-10-18)
binary: rustc
commit-hash: 09c42c45858d5f3aedfa670698275303a3d19afa
commit-date: 2021-10-18
host: x86_64-unknown-linux-musl
release: 1.56.0
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryA-threadArea: `std::thread`C-discussionCategory: Discussion or questions that doesn't represent real issues.T-testing-devexRelevant to the testing devex team (testing DX), which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions