Description
Hi @matklad !
I've been using the pattern from your post on dropping Rust threads, and recently got bitten by a bug along the lines of rust-lang/rust#48820 , but I'm mainly opening this issue to ask what you think might be a good approach to fixing this in Rust.
Coming from a mostly C and C++ background, and with a few years of mostly-hobby Rust experience, I find this situation feels very not-Rusty and surprising. Part of the problem is that, IMO, it's not well documented in either JoinHandle or Drop documentation that Rust does /not/ guarantee destructors will be called (the forget docs cover this nicely). So, even a person who reads the post, and wonders about cleanup of the worker thread, likely won't understand the problem if they look in the obvious places.
As a first thought about a fix, I'd suggest doing each of:
- Improve the JoinHandle and Drop docs, around risk of destructors not being called.
- Mark JoinHandle as #[must_use]
- Add a
fn detach(self)
method on JoinHandle
Do you think those would be reasonable fixes to propose, and if so how would you suggest going about it? Thanks for your time, your blog (it's been a great learning experience, and I still like that pattern when used carefully ;) ), and especially rust-analyzer! I use rust-analyzer daily, and recommend it whenever there's a good opportunity.