-
Notifications
You must be signed in to change notification settings - Fork 55
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 deadlock on channel mutex when stopping pool #148
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,65 @@ | ||
from concurrent.futures import FIRST_COMPLETED, wait | ||
from dataclasses import dataclass | ||
import time | ||
import unittest | ||
|
||
from pebble import ProcessPool | ||
from pebble.common.types import CONSTS, FutureStatus | ||
|
||
def function(argument, sleep_interval): | ||
time.sleep(sleep_interval) | ||
return argument | ||
|
||
g_pool = None | ||
|
||
@dataclass | ||
class SpamMessage: | ||
pass | ||
|
||
def flooding_function(): | ||
workers_channel = g_pool._pool_manager.worker_manager.workers_channel | ||
while True: | ||
workers_channel.send(SpamMessage()) | ||
|
||
class TestProcessPoolGeneric(unittest.TestCase): | ||
def test_big_values_and_cancellation(self): | ||
"""Test that the pool handles workers with big tasks and can cancel them.""" | ||
# Ideally this should be bigger than the multiprocessing pipe's internal | ||
# buffer. | ||
BIG_VALUE = [0] * 10 * 1000 * 1000 | ||
# The bigger number of workers is, the higher is the chance of catching | ||
# bugs. | ||
CNT = 50 | ||
# Let the worker events cluster around the sleep unit granularity to | ||
# increase the chance of catching bugs. | ||
INITIAL_SLEEP = CONSTS.sleep_unit * 10 | ||
EPS = CONSTS.sleep_unit / 10 | ||
|
||
futures = [] | ||
with ProcessPool(max_workers=CNT) as pool: | ||
for i in range(CNT): | ||
futures.append(pool.schedule(function, args=[BIG_VALUE, INITIAL_SLEEP + i * EPS])) | ||
wait(futures, return_when=FIRST_COMPLETED) | ||
for f in futures: | ||
f.cancel() | ||
time.sleep(EPS * CNT / 2) | ||
pool.stop() | ||
pool.join() | ||
|
||
def test_message_flood_from_worker(self): | ||
"""Test that the pool stops despite the worker spamming the message pipe.""" | ||
with ProcessPool() as pool: | ||
# Use a global variable to pass channels to the worker. | ||
global g_pool | ||
g_pool = pool | ||
|
||
future = pool.schedule(flooding_function) | ||
|
||
# Wait until the worker starts running the (spammy) task. | ||
while future._state == FutureStatus.PENDING: | ||
time.sleep(0.1) | ||
self.assertEqual(future._state, FutureStatus.RUNNING) | ||
|
||
pool.stop() | ||
pool.join() | ||
g_pool = None |
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.
I haven't found a good way to assert the state here: neither that there was no
BrokenProcessPool
inpool_manager_loop()
nor that we slept forLOCK_TIMEOUT
:PoolContext.status
setter to allow transitioning fromSTOPPED
toERROR
, but apparently there are other places that try to setERROR
even in "good" shutdown scenarios.LOCK_TIMEOUT
to some really big number in this test?..