Skip to content

[Axstd] Dropping thread::JoinHandle doesn't run the task #318

@zjp-CN

Description

@zjp-CN

Axstd mocks libstd APIs, and JoinHandle is documented as follows:

/// A `JoinHandle` *detaches* the associated thread when it is dropped, which
/// means that there is no longer any handle to the thread and no way to `join`
/// on it.
pub struct JoinHandle<T> {
native: AxTaskHandle,
thread: Thread,
packet: Arc<Packet<T>>,
}

It means when JoinHandle is dropped, the thread is still running behind the scenes. playground

However, ArceOS doesn't schedule the thread at all. Here's a demo:

# Cargo.toml
axstd = { workspace = true, features = ["multitask"] }
#![no_std]
#![no_main]

use axstd::{println, thread};
use core::sync::atomic::{AtomicU64, Ordering};

#[unsafe(no_mangle)]
fn main() {
    let n = AtomicU64::new(0);
    loop {
        let id = n.fetch_add(1, Ordering::Relaxed);

        _ = thread::spawn(move || println!("{id}")).join(); // (1)
        // drop(thread::spawn(move || println!("{id}"))); // or (2)

        if id == 10000 {
            break;
        }
    }
    println!("main stops.");
}

(1) works well because it waits for the task to finish, and all the 10000 threads are successfully run.
(2) causes OOM panic: no task is run at all, so memory runs out.

[  0.029819 0:2 axruntime::lang_items:5] 
panicked at library/alloc/src/alloc.rs:437:13: memory allocation of 262144 bytes failed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions