Repository navigation
Serialize SemLock bookkeeping for Condition notify #8739
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3e9c150
Serialize SemLock owner bookkeeping around waits
youknowone fdd78ed
Repeat the Condition.notify_all snippet 20 times
youknowone 1860498
Cover one-by-one Condition.notify in the snippet
youknowone 7c42e17
address review: bound snippet waits with timeouts
youknowone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """Thread+process Condition notify must wake every waiter.""" | ||
|
|
||
| import multiprocessing | ||
| import threading | ||
|
|
||
|
|
||
| def wait_on_condition(cond, started, woken): | ||
| cond.acquire() | ||
| started.release() | ||
| cond.wait() | ||
| woken.release() | ||
| cond.release() | ||
|
|
||
|
|
||
| def join_workers(workers): | ||
| for worker in workers: | ||
| worker.join(timeout=5) | ||
| assert not worker.is_alive() | ||
|
|
||
|
|
||
| def wait_sem(sem, msg): | ||
| assert sem.acquire(timeout=5), msg | ||
|
|
||
|
|
||
| def notify_all_six(ctx): | ||
| cond = ctx.Condition() | ||
| started = ctx.Semaphore(0) | ||
| woken = ctx.Semaphore(0) | ||
|
|
||
| workers = [] | ||
| for _ in range(3): | ||
| proc = ctx.Process(target=wait_on_condition, args=(cond, started, woken)) | ||
| proc.daemon = True | ||
| proc.start() | ||
| workers.append(proc) | ||
|
|
||
| thread = threading.Thread(target=wait_on_condition, args=(cond, started, woken)) | ||
| thread.daemon = True | ||
| thread.start() | ||
| workers.append(thread) | ||
|
|
||
| for i in range(6): | ||
| wait_sem(started, f"waiter {i} did not enter Condition.wait()") | ||
|
|
||
| cond.acquire() | ||
| cond.notify_all() | ||
| cond.release() | ||
|
|
||
| for i in range(6): | ||
| wait_sem(woken, f"waiter {i} did not wake") | ||
| join_workers(workers) | ||
|
|
||
|
|
||
| def notify_one_then_the_other(ctx): | ||
| cond = ctx.Condition() | ||
| started = ctx.Semaphore(0) | ||
| woken = ctx.Semaphore(0) | ||
|
|
||
| proc = ctx.Process(target=wait_on_condition, args=(cond, started, woken)) | ||
| proc.daemon = True | ||
| proc.start() | ||
| thread = threading.Thread(target=wait_on_condition, args=(cond, started, woken)) | ||
| thread.daemon = True | ||
| thread.start() | ||
|
|
||
| wait_sem(started, "process did not enter Condition.wait()") | ||
| wait_sem(started, "thread did not enter Condition.wait()") | ||
|
|
||
| cond.acquire() | ||
| cond.notify() | ||
| cond.release() | ||
| wait_sem(woken, "first waiter did not wake") | ||
|
|
||
| cond.acquire() | ||
| cond.notify() | ||
| cond.release() | ||
| wait_sem(woken, "second waiter did not wake") | ||
| join_workers([proc, thread]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| multiprocessing.freeze_support() | ||
| ctx = multiprocessing.get_context("spawn") | ||
| # Enough repeats to surface a lost-wakeup race in CI snippets. | ||
| for _ in range(20): | ||
| notify_all_six(ctx) | ||
| notify_one_then_the_other(ctx) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 10365
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 50377
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 13352
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 13522
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 15085
Do not hold
critduring VM callbacks.critis non-reentrant. The guard remains held whiletry_float(vm)can invoke a timeout object's__float__method and whilevm.check_signals()can invoke a Python signal handler. Either callback can call_count,acquire, orreleaseon the sameSemLock, which self-deadlocks oncrit.Move lock acquisition after timeout conversion. Drop and reacquire it around signal handling. Keep the semaphore trial and owner bookkeeping in the critical section.
🤖 Prompt for AI Agents