Skip to content

Dropping JoinHandle is problematic #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ianrrees opened this issue Aug 11, 2022 · 1 comment
Open

Dropping JoinHandle is problematic #79

ianrrees opened this issue Aug 11, 2022 · 1 comment

Comments

@ianrrees
Copy link

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.

@matklad
Copy link
Owner

matklad commented Aug 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants