Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
yoney wants to merge1 commit intopython:mainfromyoney:ft_bisect

Conversation

@yoney
Copy link
Contributor

@yoneyyoney commentedJul 22, 2025
edited by bedevere-appbot
Loading

Modify theinsort_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

Copy link
Contributor

@colesburycolesbury left a 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.

yoney reacted with thumbs up emoji
from test.support import import_helper, threading_helper, subTests
from test.support.threading_helper import run_concurrently

bisect = import_helper.import_module("bisect")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can this just beimport bisect?

If I understand correctly,import_helper.import_module() handles missing optional modules but that doesn't seem to be relevant here.

yoney reacted with thumbs up emoji
@yoney
Copy link
ContributorAuthor

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.

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.

@yoney
Copy link
ContributorAuthor

The pure Python implementations in bsiect.py are still not going to be thread safe.

Yes, pure Python version would be different, which is a good reason to consider dropping this change.
Just to clarify my understanding; there is noway to lock the mutex on the object from Python, right?

@colesbury
Copy link
Contributor

Just to clarify my understanding; there is noway to lock the mutex on the object from Python, right?

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.

yoney reacted with thumbs up emoji

@ZeroIntensity
Copy link
Member

The pure Python implementations in bsiect.py are still not going to be thread safe.

I think this is an issue with several other modules already. For example, the pure-Python implementation ofheapq is not thread-safe while the C implementation is.

@rhettingerrhettinger removed their request for reviewJuly 23, 2025 02:46
@rhettinger
Copy link
Contributor

I think this is an issue with several other modules already. For example, the pure-Python implementation of heapq is not thread-safe while the C implementation is.

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.

yoney reacted with thumbs up emoji

@colesbury
Copy link
Contributor

I think withheapq the locks were necessary to make it not segfault under race conditions because it directly accessed list internals. I don't think that's the case here because we're using the abstract sequence API instead.

yoney reacted with thumbs up emoji

@kumaraditya303
Copy link
Contributor

While these functions did not crash before, it is more intuitive for them to lock the sequence they are about to modify.

I think instead of adding locks for this, we should document the thread safety of this module like as in#136555

ZeroIntensity and yoney reacted with thumbs up emoji

@ZeroIntensity
Copy link
Member

To clarify, I'm not saying we should necessarily add locking tobisect, I just think we shouldn't reject the idea on the basis that it would create differences between the C and Python implementation, because we already do that in other modules.

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.

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).

@serhiy-storchaka
Copy link
Member

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.

ZeroIntensity reacted with thumbs up emoji

@yoney
Copy link
ContributorAuthor

I think withheapq the locks were necessary to make it not segfault under race conditions because it directly accessed list internals. I don't think that's the case here because we're using the abstract sequence API instead.

Exactly, locks were necessary forheapq because it directly accesses the internals of a list. Without the locks, it would segfault.

@yoney
Copy link
ContributorAuthor

I think instead of adding locks for this, we should document the thread safety of this module like as in#136555

I'm closing this PR in favor of documenting it. I believe#136555 will be merged soon.

Thank you all for your reviews and feedback!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@colesburycolesburycolesbury left review comments

@Yhg1sYhg1sAwaiting requested review from Yhg1s

@mpagempageAwaiting requested review from mpage

@rhettingerrhettingerAwaiting requested review from rhettingerrhettinger is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

6 participants

@yoney@colesbury@ZeroIntensity@rhettinger@kumaraditya303@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp