-
Notifications
You must be signed in to change notification settings - Fork 817
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
Avoid spinlocks #916
Comments
Thanks for that, that's a good article. I think we should probably return to |
This crate did originally (2017-2020) use a spinlock, using the @programmerjake Are you actually experiencing any problems with this? Python isn't really usable with multiple threads anyway, with the global interpreter lock. So yes, you can send You could simply replace |
I also don't think this is so problematic since |
Hello, today we have moved in our project to the latest version 0.10, and it seems that spinlock dead lock is not so rare as you may expected, since we found it straight out after update - python process consumes 100% of CPU. I've attached sample of this process, and after remove spinlock from the project and returning
|
@IvanKuzavkov |
@kngwyu thanks. |
Hmm but deadlock sounds weird... 🤔 |
here is the diff: |
That diff is not the same behaviour, as the locks are held for much shorter duration, and notably not even during the I think the issue looks related to @IvanKuzavkov are you able to provide a minimum reproduction of the test which causes problems for you on |
EDIT: Ahh I think I see why this deadlock is occurring. The problem is this line being called during drop when the current thread already is already in an I'm suprised no tests flagged this. I will write a patch fix and tests tonight (~ 8 hrs time). If someone else wants this fixed sooner, please take this on. |
How about using swap(=original solution) or {
let v = self.pointers_to_incref.lock().split_off(0);
for ptr in v {
unsafe { ffi::Py_INCREF(ptr.as_ptr()); }
}
} |
@dvidhewitt hello, I think providing good example can be a thing, but I can implement right fix, if it's necessary. Also if using spinlock requires additional recursive lock check, maybe rollback to mutex solution isn't a bad idea, to traid some performance to code stability? |
Yeah, a good fix would be to get the lock, swap the two vectors for empty ones (similar to how you use
It's not spinlock fault, you are holding the Mutex for much less time in the diff you provided. Mutex implemented safely would also have the same deadlock. |
Yeah, you right, holding |
FWIW, after consideration in #924 we decided to use a @m-ou-se appreciate you may not be happy to see the additional dependency reintroduced. Willing to work to introduce a |
@davidhewitt No worries, looks like this is the best solution right now. :) Happy this problem was found and fixed so quickly. |
See https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html for some reasoning.
TLDR: spinlocks cause issues because the OS doesn't know when something is blocked, so can cause awful scheduling issues and big time delays. spinlocks are basically only appropriate for embedded or OS kernel software, and then you have to be really careful.
See also: #899
The text was updated successfully, but these errors were encountered: