-
Notifications
You must be signed in to change notification settings - Fork 37
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
shuttle::thread_local isn't dropped when threads exit the scheduler #86
Comments
Hmm, this is supposed to work — we go to some trouble to run thread-local destructors when a thread finishes, and we have a test for thread-locals dropping at the right time. I'll try to dig into this a bit more. Thanks for the report! |
is maybe a simpler reproduction of the reported deadlock. The std variant of the same test function doesn't deadlock. Maybe this is out of scope - turns out if you remove the spawn's thread_local then it will actually panic instead!
The Mutex (and channel, in my original example) are both outliving the shuttle scheduler since the static_local and 'static Mutex escape its closure, is maybe the root of the problem... If shuttle provided its own lazy_static that is re-initialized and dropped by the thread scheduler so the concurrency primitives don't escape this would also probably be less of an issue, though maybe that's just a poor man's version of #81 |
Actually, you can probably just make an mpsc channel inside the shuttle::check_random scope and hand the receiver to a scoped thread to block on recv, and put the sender in a thread_local. In which case no concurrency primitives have a 'static lifetime or are escaping the closure... |
We weren't running the same TLS destructor logic for the main thread as all other threads. This meant a thread-local on the main thread wouldn't be dropped until the entire test finished, causing spurious deadlocks or other confusion. Fixes awslabs#86.
I think #88 will fix this. We weren't running the thread-local destructors for the main thread until the entire test ended. In your example that caused both spurious deadlocks (if the main thread held the lock) and the weird |
We weren't running the same TLS destructor logic for the main thread as all other threads. This meant a thread-local on the main thread wouldn't be dropped until the entire test finished, causing spurious deadlocks or other confusion. Fixes #86.
We weren't running the same TLS destructor logic for the main thread as all other threads. This meant a thread-local on the main thread wouldn't be dropped until the entire test finished, causing spurious deadlocks or other confusion. Fixes #86.
I have a crate that setups up cross-thread channels via
thread_local
andlazy_static
. Shuttle reports a spurious deadlock, however, because it only frees the thread_local value for threads when they actually exit and not when they "exit" the thread scheduler (such as hitting the end ofshuttle::check_random(|| { })
) - I am using the thread_local Drop impl causing the last sender of a channel to go away to unblock another thread's receiver, so the other thread can exit once there are no more clients. Because thread_locals don't drop, the sender is never removed, and so the receiver is reported as a deadlock, despite this not being possible in normal operations.I have a manual workaround, where I just
RefCell::take()
the channel at the end of my shuttle tests in order to manually drop the sender, but it would be nice to not need this (and it took me more than a little while to figure out what exactly was going on and realize the problem, which may hit other people).The text was updated successfully, but these errors were encountered: