-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
thread::park
causes undefined behaviour on panic
#102398
Comments
Thanks for bringing this up! I agree this is a problem. In general, the pattern with parking is: while !some_condition {
thread::park();
} Unwinding would mean leaving that I think it's not reasonable to expect every user of |
All the situations in which |
I think Rust should allocate the needed resources eagerly when creating a new thread, to avoid this problem. |
It actually does most of the time, as In general, I think it is easier for maintainability if we just always convert panics to aborts in |
I just tripped over an issue in this area. I'm trying to put together a userspace mutex for learning and it works fine apart from when there is a panic. If there has been a panic, then I get a panic when handling panic and fall over with this stack trace:
Note that at 18, some destructors are run, that appear to be releasing Thread local storage. By the time I try to run
But, I get the same problem. I presume because of raciness between the check for panicking and the panic actually starting. |
@garypen It looks like you're implementing a global allocator using your thread::park_timeout-based mutex? That's going to be problematic, since the thread parking data is stored in a heap allocation (an |
Never panic in `thread::park` and `thread::park_timeout` fixes rust-lang#102398 ``@rustbot`` label +T-libs +T-libs-api
@m-ou-se Thank you. I have an approach to the problem which works as a spinlock, but I was experimenting to see if I could do something more efficient using |
A lot of code dealing with synchronization assumes that
thread::park
does not panic. This assumption is however not correct. For instance, a panic occurs if the TLS key used to store the thread info cannot be created, if a new thread id cannot be created, if the creation of a parking primitive fails, if the current time is not available and if the parking operation fails.The code examples above all use intrusive lists and have to wait on a condition to before destroying the list nodes. If
thread::park
panics, the nodes will be dropped during stack unwinding without checking that condition, leading to undefined behaviour (some of these cases are instd
, so I'm marking this as a soundness problem withstd
).Fixing this would either require guarding against panics in each of these cases or ensuring that
thread::park
never panics.@rustbot label +I-unsound +A-atomic +T-libs +T-libs-api
The text was updated successfully, but these errors were encountered: