-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Assertion failure when locking a mutex in a c thread (linux) #17485
Comments
I can't open your file. Can you put the code in a GitHub repo instead so I can browse it and clone it? |
Good idea! Sorry, I didn't think of that. Anyway, here it is: https://github.com/blastrock/rustcrash |
I think I found it. It comes down to a thread-local variable not being initialized. It is located in librustrt/local_ptr.rs:
This variable should be initialized by Task, but since we come from a c context, there is no such Task. I think that I was wrong using a Mutex here since they may pause threads and schedule another task over it, which is not what we want when we come from a c context (which may have locked native mutexes or rely on thread-local storage). My code seems to work if I use std::rt::mutex::NativeMutex. If this is the way to go, maybe this documentation should be updated: http://doc.rust-lang.org/guide-ffi.html#asynchronous-callbacks and the error message should be made more explicit. |
This is working as designed. It's undefined behaviour to call libstd on threads created outside of the libstd API. You need to use |
I'm not writing a library with a C API, I'm just using a C library. In the documentation link I provided, it is said that it is possible (and I managed to do it with either NativeMutexes or channels). If it is not officially possible, then the documentation should say it. |
You're running code from the standard library on a thread it didn't create without manually initializing the runtime on it. That's explicitly not permitted and it is documented. |
Maybe you're right, but I can't find that part in the documentation. What I did read is
|
You're free to file bugs about inaccurate parts of the documentation, but the undefined behaviour you're getting is not a bug and is documented elsewhere. |
Using Rust in a C callback with the standard library implies the following:
Anything else has undefined behaviour. |
…=alibektas minor : Fix duplicate snippets showing up on hover. With each `config::apply_change` duplicate configs were being added. Now we first drain the vec that holds these and then start adding. This fixes rust-lang#17485
Hi,
I get an assertion failure when I try to lock a mutex from a thread created from C++ (I think it is reproducible with c). I made a minimal example here: https://github.com/blastrock/rustcrash
Uncompress the archive and type "make run". On my machine it finishes with:
EDIT: Here is the stack trace:
The text was updated successfully, but these errors were encountered: