-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-116738: Make bisect module thread-safe #137021
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pure Python implementations in bsiect.py are still not going to be thread safe.
While these functions did not crash before, it is more intuitive for them to lock the sequence they are about to modify...
I'm not fully convinced this is worth it. I think it's okay for bisect not to be thread safe, but I don't feel very strongly about this one way or the other.
from test.support import import_helper, threading_helper, subTests | ||
from test.support.threading_helper import run_concurrently | ||
|
||
bisect = import_helper.import_module("bisect") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this just be import bisect
?
If I understand correctly, import_helper.import_module()
handles missing optional modules but that doesn't seem to be relevant here.
I don't feel strongly about landing it. If we agree current state is good enough, we can go ahead and mark the module as safe and close the PR. |
Yes, pure Python version would be different, which is a good reason to consider dropping this change. |
No, we don't expose a way to lock the object. Critical sections are not very useful from Python because the eval breaker checks can run arbitrary code (such as via the GC) and that can temporarily suspend the active critical section. So things would only be "atomic most of the time", which is not a very useful property. |
I think this is an issue with several other modules already. For example, the pure-Python implementation of |
ISTM that all we want in these cases is for the C implementation to not segfault. There is no need to promise more than the pure python version promises. If the heapq module was edited to do more, that should probably be reverted. The C versions are intended to just be accelerators. |
I think with |
I think instead of adding locks for this, we should document the thread safety of this module like as in #136555 |
To clarify, I'm not saying we should necessarily add locking to
The problem is that by adding memory safety to things like heapq (i.e., making it not crash via locking), we're also incidentally adding thread safety. But, then the Python implementation remains memory safe without thread safety (i.e., doesn't crash but also doesn't really work). |
If we make the C implementation thread-safe, users will start writing code that depends on this and won't work with a pure Python implementation. |
Modify the
insort_left()
andinsort_right()
functions in thebisect
module to be thread-{aware,safe} by adding a lock to the sequence argument. While these functions did not crash before, it is more intuitive for them to lock the sequence they are about to modify. These functions locate the correct position to insert an item and then perform the insertion. By adding a lock, the sequence remains safe from concurrent modifications by other threads during this process.The new tests failed before introducing the lock.
cc: @mpage @Yhg1s @colesbury