Skip to content
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

Waiting for threadpool threads to drop #1470

Open
PvdBerg1998 opened this issue Mar 6, 2019 · 4 comments
Open

Waiting for threadpool threads to drop #1470

PvdBerg1998 opened this issue Mar 6, 2019 · 4 comments
Labels
A-executor Area: futures::executor C-feature-request

Comments

@PvdBerg1998
Copy link

PvdBerg1998 commented Mar 6, 2019

I have some shared data in an Arc that I want to drop when my program exits.
However, when I drop the ThreadPool, this only sends a Close message to the worker threads but does not join them. This means I have no way to block until I know the threads/shared data is dropped.

Edit: Tokio's threadpool does not have this issue.

@PvdBerg1998
Copy link
Author

I tried just saving all the thread handles and joining them in drop, but this doesn't work. This is probably because the WakeHandle gets passed down, which also contains an Arc. This should probably be a Weak though, since waking a future in a dropped executor makes no sense anyway.

@PvdBerg1998
Copy link
Author

The behaviour I'm seeing is that futures are not dropped after the threadpool is dropped.

@PvdBerg1998
Copy link
Author

PvdBerg1998 commented Mar 6, 2019

// Let there be some shared data in an Arc
let shared_data = Arc::new(data);
// Let there be a future that has a strong reference to this data
let future = SomeFuture::new(shared_data.clone());
// Spawn it on the threadpool
threadpool.spawn(future);

// The threadpool Drop implementation has been patched to join every thread
drop(threadpool);
// Now all the threadpool threads are dropped

// So logically, we are the only owner of the shared data Arc now?
assert!(Arc::strong_count(&shared_data) == 1);
// PANIC!

// Replacing the assert with a while loop:
// Note that this actually returns after a very short time
while Arc::strong_count(&shared_data) != 1 {}
// Returns.

@PvdBerg1998
Copy link
Author

I have created a P.o.C. threadpool that does not have this issue: https://github.com/PvdBerg1998/futures-executor

SrTobi added a commit to SrTobi-Forks/futures-rs that referenced this issue Jul 16, 2019
Adds an option to ThreadPoolBuilder to configure the resulting ThreadPool
to join the worker threads when dropped.

fixes rust-lang#1470
@taiki-e taiki-e added the A-executor Area: futures::executor label Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-executor Area: futures::executor C-feature-request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants