std: multithreaded crash during thread init #41

Open
opened 2025-03-02 15:17:45 +00:00 by alnyan · 2 comments
Owner

Program: tst

Source:

use std::{thread, time::Duration};

fn main() {
    let mut threads = vec![];
    for i in 0..4 {
        let jh = thread::Builder::new()
            .name(format!("tst-thread-{i}"))
            .spawn(move || {
                let current = thread::current();
                for _ in 0..100 {
                    println!("Hi from thread {:?}", current.name());
                    thread::sleep(Duration::from_secs(1));
                }
            })
            .unwrap();

        threads.push(jh);
    }

    for thread in threads.into_iter() {
        thread.join().unwrap();
    }
}

Log:

000016:0:libk::task::binary::elf:192: Loading ELF virtual 0x0..0x1d000 -> real 0x19fce000..0x19feb000
000016:0:libk::task::binary:185: stack: 0x3000000..0x3020000
Hi from thread Some("tst-thread-0")
000016:2:ptrace:tst (21) thread '
Hi from thread Some("tst-thread-1")
thread '000016Hi from thread Some("tst-thread-3")
:2:ptrace:tst (21) tst-thread-2
tst-thread-2000016:2:ptrace:tst (21) ' panicked at
' panicked at 000016:2:ptrace:tst (21) std/src/sync/once.rs
std/src/sync/once.rs000016:2:ptrace:tst (21) :
:000016:2:ptrace:tst (21) 217
217000016:2:ptrace:tst (21) :
:000016:2:ptrace:tst (21) 20
20000016:2:ptrace:tst (21) :

:
000016:2:ptrace:tst (21) one-time initialization may not be performed recursively
one-time initialization may not be performed recursively000016:2:ptrace:tst (21)


000016:2:ptrace:tst (21) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
000016:2:libk::task::thread:373: #26: raise signal Aborted
000016:2:yggdrasil_kernel::arch::x86_64::exception:127: InvalidOpcode in #26 ("tst-thread-2")
000016:2:yggdrasil_kernel::arch::x86_64::exception:128: ip = 23:19fe0164
000016:2:yggdrasil_kernel::arch::x86_64::exception:129: cr3 = 0x055b1000
000016:2:ptrace:tst (21) runtime: terminated by signal: Aborted
Program: `tst` Source: ```rust use std::{thread, time::Duration}; fn main() { let mut threads = vec![]; for i in 0..4 { let jh = thread::Builder::new() .name(format!("tst-thread-{i}")) .spawn(move || { let current = thread::current(); for _ in 0..100 { println!("Hi from thread {:?}", current.name()); thread::sleep(Duration::from_secs(1)); } }) .unwrap(); threads.push(jh); } for thread in threads.into_iter() { thread.join().unwrap(); } } ``` Log: ``` 000016:0:libk::task::binary::elf:192: Loading ELF virtual 0x0..0x1d000 -> real 0x19fce000..0x19feb000 000016:0:libk::task::binary:185: stack: 0x3000000..0x3020000 Hi from thread Some("tst-thread-0") 000016:2:ptrace:tst (21) thread ' Hi from thread Some("tst-thread-1") thread '000016Hi from thread Some("tst-thread-3") :2:ptrace:tst (21) tst-thread-2 tst-thread-2000016:2:ptrace:tst (21) ' panicked at ' panicked at 000016:2:ptrace:tst (21) std/src/sync/once.rs std/src/sync/once.rs000016:2:ptrace:tst (21) : :000016:2:ptrace:tst (21) 217 217000016:2:ptrace:tst (21) : :000016:2:ptrace:tst (21) 20 20000016:2:ptrace:tst (21) : : 000016:2:ptrace:tst (21) one-time initialization may not be performed recursively one-time initialization may not be performed recursively000016:2:ptrace:tst (21) 000016:2:ptrace:tst (21) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 000016:2:libk::task::thread:373: #26: raise signal Aborted 000016:2:yggdrasil_kernel::arch::x86_64::exception:127: InvalidOpcode in #26 ("tst-thread-2") 000016:2:yggdrasil_kernel::arch::x86_64::exception:128: ip = 23:19fe0164 000016:2:yggdrasil_kernel::arch::x86_64::exception:129: cr3 = 0x055b1000 000016:2:ptrace:tst (21) runtime: terminated by signal: Aborted ```
alnyan added the
bug
crash
scope
userspace-std
labels 2025-03-02 15:17:46 +00:00
Author
Owner

Binary that crashes

Binary that crashes
59 KiB
Author
Owner

Doesn't reproduce on x86-64/riscv64, now reproduces on aarch64:

/ $ tst
000001:0:ptrace:tst (37)
thread 'tst-thread-3' (42) panicked at library/std/src/sync/once.rs:219:20:
one-time initialization may not be performed recursively

Hi from thread Some("Hi from thread tst-thread-2")

thread 'tst-thread-3' (42) panicked at library/std/src/sync/once.rs:219:20:
one-time initialization may not be performed recursively
000001:0:ptrace:tst (37) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
000001:0:yggdrasil_kernel::arch::aarch64::exception:261: Thread #42 "tst-thread-3" hit a breakpoint
000001:1:ptrace:tst (37)
thread 'tst-thread-1' (40) panicked at library/core/src/cell.rs:2031:9:
assertion failed: is_writing(borrow)

000001:0:libk::task::thread
thread 'tst-thread-1' (40) panicked at library/core/src/cell.rs:2031:9:
assertion failed: is_writing(borrow)
:373: #42: raise signal Aborted
000001:1:yggdrasil_kernel::arch::aarch64::exception:261: Thread #40 "tst-thread-1" hit a breakpoint
000001:1:libk::task::thread:373: #40: raise signal Aborted
000001:0:ptrace:tst (37) runtime: terminated by signal: Aborted
000001:1:ptrace:tst (37) runtime: terminated by signal: Aborted
Hi from thread Some("tst-thread-2")
Hi from thread Some("tst-thread-2")
Hi from thread Some("tst-thread-2")
Hi from thread Some("tst-thread-2")
Doesn't reproduce on x86-64/riscv64, now reproduces on aarch64: ``` / $ tst 000001:0:ptrace:tst (37) thread 'tst-thread-3' (42) panicked at library/std/src/sync/once.rs:219:20: one-time initialization may not be performed recursively Hi from thread Some("Hi from thread tst-thread-2") thread 'tst-thread-3' (42) panicked at library/std/src/sync/once.rs:219:20: one-time initialization may not be performed recursively 000001:0:ptrace:tst (37) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 000001:0:yggdrasil_kernel::arch::aarch64::exception:261: Thread #42 "tst-thread-3" hit a breakpoint 000001:1:ptrace:tst (37) thread 'tst-thread-1' (40) panicked at library/core/src/cell.rs:2031:9: assertion failed: is_writing(borrow) 000001:0:libk::task::thread thread 'tst-thread-1' (40) panicked at library/core/src/cell.rs:2031:9: assertion failed: is_writing(borrow) :373: #42: raise signal Aborted 000001:1:yggdrasil_kernel::arch::aarch64::exception:261: Thread #40 "tst-thread-1" hit a breakpoint 000001:1:libk::task::thread:373: #40: raise signal Aborted 000001:0:ptrace:tst (37) runtime: terminated by signal: Aborted 000001:1:ptrace:tst (37) runtime: terminated by signal: Aborted Hi from thread Some("tst-thread-2") Hi from thread Some("tst-thread-2") Hi from thread Some("tst-thread-2") Hi from thread Some("tst-thread-2") ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: alnyan/yggdrasil#41