| 1 |
import os |
| 2 |
import signal |
| 3 |
import time |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import billiard.pool |
| 8 |
from billiard import get_context |
| 9 |
from billiard.exceptions import TimeLimitExceeded |
| 10 |
|
| 11 |
|
| 12 |
def func(x): |
| 13 |
if x == 2: |
| 14 |
raise ValueError |
| 15 |
return x |
| 16 |
|
| 17 |
|
| 18 |
def get_on_ready_count(): |
| 19 |
import inspect |
| 20 |
worker = inspect.stack()[1].frame.f_locals['self'] |
| 21 |
return worker.on_ready_counter.value |
| 22 |
|
| 23 |
def simple_task(x): |
| 24 |
return x * 2 |
| 25 |
|
| 26 |
def raise_base_exception(): |
| 27 |
raise BaseException("base exception test") |
| 28 |
|
| 29 |
|
| 30 |
def raise_system_exit(): |
| 31 |
raise SystemExit(1) |
| 32 |
|
| 33 |
class test_pool: |
| 34 |
def test_memory_error_from_callback_propagates(self): |
| 35 |
def callback(value): |
| 36 |
raise MemoryError(value) |
| 37 |
|
| 38 |
result = billiard.pool.ApplyResult({}, callback) |
| 39 |
|
| 40 |
with pytest.raises(MemoryError, match='out of memory'): |
| 41 |
result._set(None, (True, 'out of memory')) |
| 42 |
|
| 43 |
def test_raises(self): |
| 44 |
pool = billiard.pool.Pool() |
| 45 |
assert pool.did_start_ok() is True |
| 46 |
pool.close() |
| 47 |
pool.terminate() |
| 48 |
|
| 49 |
def test_timeout_handler_iterates_with_cache(self): |
| 50 |
# Given a pool |
| 51 |
pool = billiard.pool.Pool() |
| 52 |
# If I have a cache containing async results |
| 53 |
cache = {n: pool.apply_async(n) for n in range(4)} |
| 54 |
# And a TimeoutHandler with that cache |
| 55 |
timeout_handler = pool.TimeoutHandler(pool._pool, cache, 0, 0) |
| 56 |
# If I call to handle the timeouts I expect no exception |
| 57 |
next(timeout_handler.handle_timeouts()) |
| 58 |
|
| 59 |
def test_exception_traceback_present(self): |
| 60 |
pool = billiard.pool.Pool(1) |
| 61 |
results = [pool.apply_async(func, (i,)) for i in range(3)] |
| 62 |
|
| 63 |
time.sleep(1) |
| 64 |
pool.close() |
| 65 |
pool.join() |
| 66 |
pool.terminate() |
| 67 |
|
| 68 |
for i, res in enumerate(results): |
| 69 |
if i == 2: |
| 70 |
with pytest.raises(ValueError): |
| 71 |
res.get() |
| 72 |
|
| 73 |
def test_base_exception_propagates(self): |
| 74 |
pool = billiard.pool.Pool(1) |
| 75 |
result = pool.apply_async(raise_base_exception) |
| 76 |
|
| 77 |
with pytest.raises(BaseException, match="base exception test"): |
| 78 |
result.get(timeout=10) |
| 79 |
|
| 80 |
pool.close() |
| 81 |
pool.join() |
| 82 |
pool.terminate() |
| 83 |
|
| 84 |
def test_system_exit_from_task_replaces_worker(self): |
| 85 |
pool = billiard.pool.Pool(1) |
| 86 |
try: |
| 87 |
pid_before = pool.apply_async(os.getpid).get(timeout=10) |
| 88 |
result = pool.apply_async(raise_system_exit) |
| 89 |
with pytest.raises(SystemExit): |
| 90 |
result.get(timeout=10) |
| 91 |
pid_after = pool.apply_async(os.getpid).get(timeout=10) |
| 92 |
assert pid_after != pid_before |
| 93 |
finally: |
| 94 |
pool.terminate() |
| 95 |
pool.join() |
| 96 |
|
| 97 |
def test_hard_timeout_does_not_stall_pool(self): |
| 98 |
pool = billiard.pool.Pool(1, timeout=1) |
| 99 |
try: |
| 100 |
pid_before = pool.apply_async(os.getpid).get(timeout=10) |
| 101 |
result = pool.apply_async(time.sleep, (30,)) |
| 102 |
with pytest.raises(Exception) as excinfo: |
| 103 |
result.get(timeout=10) |
| 104 |
exc = getattr(excinfo.value, 'exc', excinfo.value) |
| 105 |
assert isinstance(exc, TimeLimitExceeded) |
| 106 |
# Only submit again once the killed worker has been replaced; |
| 107 |
# otherwise the job may still be picked up by the dying worker. |
| 108 |
deadline = time.monotonic() + 10 |
| 109 |
while time.monotonic() < deadline: |
| 110 |
if pid_before not in [w.pid for w in pool._pool]: |
| 111 |
break |
| 112 |
time.sleep(0.1) |
| 113 |
assert pool.apply_async(simple_task, (21,)).get(timeout=10) == 42 |
| 114 |
except BaseException: |
| 115 |
# A stalled pool cannot be terminated either (terminate() blocks |
| 116 |
# on the same inqueue lock), so kill the workers outright and |
| 117 |
# skip the terminate-at-exit finalizer. |
| 118 |
for worker in pool._pool: |
| 119 |
try: |
| 120 |
os.kill(worker.pid, signal.SIGKILL) |
| 121 |
except OSError: |
| 122 |
pass |
| 123 |
pool._terminate.cancel() |
| 124 |
raise |
| 125 |
pool.terminate() |
| 126 |
pool.join() |
| 127 |
|
| 128 |
def test_on_ready_counter_is_synchronized(self): |
| 129 |
for ctx in ('spawn', 'fork', 'forkserver'): |
| 130 |
pool = billiard.pool.Pool(processes=1, context=get_context(ctx)) |
| 131 |
pool.apply_async(func, (1,)).get(1) |
| 132 |
on_ready_counter = pool.apply_async(get_on_ready_count, ).get(1) |
| 133 |
assert on_ready_counter == 1 |
| 134 |
pool.close() |
| 135 |
pool.join() |
| 136 |
pool.terminate() |
| 137 |
|
| 138 |
def test_graceful_shutdown_delivers_results(self): |
| 139 |
"""Test that queued results are delivered during pool shutdown. |
| 140 |
|
| 141 |
Specifically, this test verifies that when _terminate_pool() is called, |
| 142 |
the ResultHandler.finish_at_shutdown() continues processing results |
| 143 |
that workers have placed in the outqueue. |
| 144 |
""" |
| 145 |
|
| 146 |
# Create pool with threads=False so that the result handler thread does |
| 147 |
# not start and the task results are allowed to build up in the queue. |
| 148 |
pool = billiard.pool.Pool(processes=2, threads=False) |
| 149 |
|
| 150 |
# Submit tasks so that results are queued but not processed. |
| 151 |
results = [pool.apply_async(simple_task, (i,)) for i in range(8)] |
| 152 |
|
| 153 |
# Allow a small amount of time for tasks to complete. |
| 154 |
time.sleep(0.5) |
| 155 |
|
| 156 |
# Close and join the pool to ensure workers stop. |
| 157 |
pool.close() |
| 158 |
pool.join() |
| 159 |
|
| 160 |
# Call the _terminate_pool() class method to trigger the finish_at_shutdown() |
| 161 |
# function that will process results in the queue. Normally _terminate_pool() |
| 162 |
# is called by a Finalize object when the Pool object is destroyed. We cannot |
| 163 |
# call pool.terminate() here because it will call the Finalize object, which |
| 164 |
# won't do anything until the Pool object is destroyed at the end of this test. |
| 165 |
# We can simulate the shutdown behaviour by calling _terminate_pool() directly. |
| 166 |
billiard.pool.Pool._terminate_pool( |
| 167 |
pool._taskqueue, |
| 168 |
pool._inqueue, |
| 169 |
pool._outqueue, |
| 170 |
pool._pool, |
| 171 |
pool._worker_handler, |
| 172 |
pool._task_handler, |
| 173 |
pool._result_handler, |
| 174 |
pool._cache, |
| 175 |
pool._timeout_handler, |
| 176 |
pool._help_stuff_finish_args() |
| 177 |
) |
| 178 |
|
| 179 |
# Cancel the Finalize object to prevent _terminate_pool() from being called |
| 180 |
# a second time when the Pool object is destroyed. |
| 181 |
pool._terminate.cancel() |
| 182 |
|
| 183 |
# Verify that all results were delivered by finish_at_shutdown() and can be |
| 184 |
# retrieved. |
| 185 |
for i, result in enumerate(results): |
| 186 |
assert result.get() == i * 2 |