| 1 |
import signal |
| 2 |
import sys |
| 3 |
import threading |
| 4 |
import time |
| 5 |
import unittest |
| 6 |
from concurrent import futures |
| 7 |
|
| 8 |
from test import support |
| 9 |
from test.support.script_helper import assert_python_ok |
| 10 |
|
| 11 |
from .util import ( |
| 12 |
BaseTestCase, ThreadPoolMixin, ProcessPoolForkMixin, |
| 13 |
ProcessPoolForkserverMixin, ProcessPoolSpawnMixin, |
| 14 |
create_executor_tests, setup_module) |
| 15 |
|
| 16 |
|
| 17 |
def sleep_and_print(t, msg): |
| 18 |
time.sleep(t) |
| 19 |
print(msg) |
| 20 |
sys.stdout.flush() |
| 21 |
|
| 22 |
|
| 23 |
class ExecutorShutdownTest: |
| 24 |
def test_run_after_shutdown(self): |
| 25 |
self.executor.shutdown() |
| 26 |
self.assertRaises(RuntimeError, |
| 27 |
self.executor.submit, |
| 28 |
pow, 2, 5) |
| 29 |
|
| 30 |
def test_interpreter_shutdown(self): |
| 31 |
|
| 32 |
rc, out, err = assert_python_ok('-c', """if 1: |
| 33 |
from concurrent.futures import {executor_type} |
| 34 |
from time import sleep |
| 35 |
from test.test_concurrent_futures.test_shutdown import sleep_and_print |
| 36 |
if __name__ == "__main__": |
| 37 |
context = '{context}' |
| 38 |
if context == "": |
| 39 |
t = {executor_type}(5) |
| 40 |
else: |
| 41 |
from multiprocessing import get_context |
| 42 |
context = get_context(context) |
| 43 |
t = {executor_type}(5, mp_context=context) |
| 44 |
t.submit(sleep_and_print, 1.0, "apple") |
| 45 |
""".format(executor_type=self.executor_type.__name__, |
| 46 |
context=getattr(self, "ctx", ""))) |
| 47 |
|
| 48 |
|
| 49 |
self.assertFalse(err) |
| 50 |
self.assertEqual(out.strip(), b"apple") |
| 51 |
|
| 52 |
def test_submit_after_interpreter_shutdown(self): |
| 53 |
|
| 54 |
rc, out, err = assert_python_ok('-c', """if 1: |
| 55 |
import atexit |
| 56 |
@atexit.register |
| 57 |
def run_last(): |
| 58 |
try: |
| 59 |
t.submit(id, None) |
| 60 |
except RuntimeError: |
| 61 |
print("runtime-error") |
| 62 |
raise |
| 63 |
from concurrent.futures import {executor_type} |
| 64 |
if __name__ == "__main__": |
| 65 |
context = '{context}' |
| 66 |
if not context: |
| 67 |
t = {executor_type}(5) |
| 68 |
else: |
| 69 |
from multiprocessing import get_context |
| 70 |
context = get_context(context) |
| 71 |
t = {executor_type}(5, mp_context=context) |
| 72 |
t.submit(id, 42).result() |
| 73 |
""".format(executor_type=self.executor_type.__name__, |
| 74 |
context=getattr(self, "ctx", ""))) |
| 75 |
|
| 76 |
|
| 77 |
self.assertIn("RuntimeError: cannot schedule new futures", err.decode()) |
| 78 |
self.assertEqual(out.strip(), b"runtime-error") |
| 79 |
|
| 80 |
def test_hang_issue12364(self): |
| 81 |
fs = [self.executor.submit(time.sleep, 0.1) for _ in range(50)] |
| 82 |
self.executor.shutdown() |
| 83 |
for f in fs: |
| 84 |
f.result() |
| 85 |
|
| 86 |
def test_cancel_futures(self): |
| 87 |
assert self.worker_count <= 5, "test needs few workers" |
| 88 |
fs = [self.executor.submit(time.sleep, .1) for _ in range(50)] |
| 89 |
self.executor.shutdown(cancel_futures=True) |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
cancelled = [fut for fut in fs if fut.cancelled()] |
| 94 |
self.assertGreater(len(cancelled), 20) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
others = [fut for fut in fs if not fut.cancelled()] |
| 100 |
for fut in others: |
| 101 |
self.assertTrue(fut.done(), msg=f"{fut._state=}") |
| 102 |
self.assertIsNone(fut.exception()) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
self.assertGreater(len(others), 0) |
| 108 |
|
| 109 |
def test_hang_gh83386(self): |
| 110 |
"""shutdown(wait=False) doesn't hang at exit with running futures. |
| 111 |
|
| 112 |
See https://github.com/python/cpython/issues/83386. |
| 113 |
""" |
| 114 |
if self.executor_type == futures.ProcessPoolExecutor: |
| 115 |
raise unittest.SkipTest( |
| 116 |
"Hangs, see https://github.com/python/cpython/issues/83386") |
| 117 |
|
| 118 |
rc, out, err = assert_python_ok('-c', """if True: |
| 119 |
from concurrent.futures import {executor_type} |
| 120 |
from test.test_concurrent_futures.test_shutdown import sleep_and_print |
| 121 |
if __name__ == "__main__": |
| 122 |
if {context!r}: multiprocessing.set_start_method({context!r}) |
| 123 |
t = {executor_type}(max_workers=3) |
| 124 |
t.submit(sleep_and_print, 1.0, "apple") |
| 125 |
t.shutdown(wait=False) |
| 126 |
""".format(executor_type=self.executor_type.__name__, |
| 127 |
context=getattr(self, 'ctx', None))) |
| 128 |
self.assertFalse(err) |
| 129 |
self.assertEqual(out.strip(), b"apple") |
| 130 |
|
| 131 |
def test_hang_gh94440(self): |
| 132 |
"""shutdown(wait=True) doesn't hang when a future was submitted and |
| 133 |
quickly canceled right before shutdown. |
| 134 |
|
| 135 |
See https://github.com/python/cpython/issues/94440. |
| 136 |
""" |
| 137 |
if not hasattr(signal, 'alarm'): |
| 138 |
raise unittest.SkipTest( |
| 139 |
"Tested platform does not support the alarm signal") |
| 140 |
|
| 141 |
def timeout(_signum, _frame): |
| 142 |
raise RuntimeError("timed out waiting for shutdown") |
| 143 |
|
| 144 |
kwargs = {} |
| 145 |
if getattr(self, 'ctx', None): |
| 146 |
kwargs['mp_context'] = self.get_context() |
| 147 |
executor = self.executor_type(max_workers=1, **kwargs) |
| 148 |
executor.submit(int).result() |
| 149 |
old_handler = signal.signal(signal.SIGALRM, timeout) |
| 150 |
try: |
| 151 |
signal.alarm(5) |
| 152 |
executor.submit(int).cancel() |
| 153 |
executor.shutdown(wait=True) |
| 154 |
finally: |
| 155 |
signal.alarm(0) |
| 156 |
signal.signal(signal.SIGALRM, old_handler) |
| 157 |
|
| 158 |
|
| 159 |
class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase): |
| 160 |
def test_threads_terminate(self): |
| 161 |
def acquire_lock(lock): |
| 162 |
lock.acquire() |
| 163 |
|
| 164 |
sem = threading.Semaphore(0) |
| 165 |
for i in range(3): |
| 166 |
self.executor.submit(acquire_lock, sem) |
| 167 |
self.assertEqual(len(self.executor._threads), 3) |
| 168 |
for i in range(3): |
| 169 |
sem.release() |
| 170 |
self.executor.shutdown() |
| 171 |
for t in self.executor._threads: |
| 172 |
t.join() |
| 173 |
|
| 174 |
def test_context_manager_shutdown(self): |
| 175 |
with futures.ThreadPoolExecutor(max_workers=5) as e: |
| 176 |
executor = e |
| 177 |
self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 178 |
[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 179 |
|
| 180 |
for t in executor._threads: |
| 181 |
t.join() |
| 182 |
|
| 183 |
def test_del_shutdown(self): |
| 184 |
executor = futures.ThreadPoolExecutor(max_workers=5) |
| 185 |
res = executor.map(abs, range(-5, 5)) |
| 186 |
threads = executor._threads |
| 187 |
del executor |
| 188 |
|
| 189 |
for t in threads: |
| 190 |
|
| 191 |
|
| 192 |
while t.is_alive(): |
| 193 |
support.gc_collect() |
| 194 |
t.join(0.01) |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
assert all([r == abs(v) for r, v in zip(res, range(-5, 5))]) |
| 199 |
|
| 200 |
def test_shutdown_no_wait(self): |
| 201 |
|
| 202 |
|
| 203 |
executor = futures.ThreadPoolExecutor(max_workers=5) |
| 204 |
res = executor.map(abs, range(-5, 5)) |
| 205 |
threads = executor._threads |
| 206 |
executor.shutdown(wait=False) |
| 207 |
for t in threads: |
| 208 |
t.join() |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
assert all([r == abs(v) for r, v in zip(res, range(-5, 5))]) |
| 213 |
|
| 214 |
|
| 215 |
def test_thread_names_assigned(self): |
| 216 |
executor = futures.ThreadPoolExecutor( |
| 217 |
max_workers=5, thread_name_prefix='SpecialPool') |
| 218 |
executor.map(abs, range(-5, 5)) |
| 219 |
threads = executor._threads |
| 220 |
del executor |
| 221 |
|
| 222 |
for t in threads: |
| 223 |
self.assertRegex(t.name, r'^SpecialPool_[0-4]$') |
| 224 |
|
| 225 |
|
| 226 |
while t.is_alive(): |
| 227 |
support.gc_collect() |
| 228 |
t.join(0.01) |
| 229 |
|
| 230 |
def test_thread_names_default(self): |
| 231 |
executor = futures.ThreadPoolExecutor(max_workers=5) |
| 232 |
executor.map(abs, range(-5, 5)) |
| 233 |
threads = executor._threads |
| 234 |
del executor |
| 235 |
|
| 236 |
for t in threads: |
| 237 |
|
| 238 |
|
| 239 |
self.assertRegex(t.name, r'ThreadPoolExecutor-\d+_[0-4]$') |
| 240 |
|
| 241 |
|
| 242 |
while t.is_alive(): |
| 243 |
support.gc_collect() |
| 244 |
t.join(0.01) |
| 245 |
|
| 246 |
def test_cancel_futures_wait_false(self): |
| 247 |
|
| 248 |
|
| 249 |
rc, out, err = assert_python_ok('-c', """if True: |
| 250 |
from concurrent.futures import ThreadPoolExecutor |
| 251 |
from test.test_concurrent_futures.test_shutdown import sleep_and_print |
| 252 |
if __name__ == "__main__": |
| 253 |
t = ThreadPoolExecutor() |
| 254 |
t.submit(sleep_and_print, .1, "apple") |
| 255 |
t.shutdown(wait=False, cancel_futures=True) |
| 256 |
""") |
| 257 |
|
| 258 |
|
| 259 |
self.assertFalse(err) |
| 260 |
|
| 261 |
|
| 262 |
self.assertIn(out.strip(), [b"apple", b""]) |
| 263 |
|
| 264 |
|
| 265 |
class ProcessPoolShutdownTest(ExecutorShutdownTest): |
| 266 |
def test_processes_terminate(self): |
| 267 |
def acquire_lock(lock): |
| 268 |
lock.acquire() |
| 269 |
|
| 270 |
mp_context = self.get_context() |
| 271 |
if mp_context.get_start_method(allow_none=False) == "fork": |
| 272 |
|
| 273 |
expected_num_processes = self.worker_count |
| 274 |
else: |
| 275 |
expected_num_processes = 3 |
| 276 |
|
| 277 |
sem = mp_context.Semaphore(0) |
| 278 |
for _ in range(3): |
| 279 |
self.executor.submit(acquire_lock, sem) |
| 280 |
self.assertEqual(len(self.executor._processes), expected_num_processes) |
| 281 |
for _ in range(3): |
| 282 |
sem.release() |
| 283 |
processes = self.executor._processes |
| 284 |
self.executor.shutdown() |
| 285 |
|
| 286 |
for p in processes.values(): |
| 287 |
p.join() |
| 288 |
|
| 289 |
def test_context_manager_shutdown(self): |
| 290 |
with futures.ProcessPoolExecutor( |
| 291 |
max_workers=5, mp_context=self.get_context()) as e: |
| 292 |
processes = e._processes |
| 293 |
self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 294 |
[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 295 |
|
| 296 |
for p in processes.values(): |
| 297 |
p.join() |
| 298 |
|
| 299 |
def test_del_shutdown(self): |
| 300 |
executor = futures.ProcessPoolExecutor( |
| 301 |
max_workers=5, mp_context=self.get_context()) |
| 302 |
res = executor.map(abs, range(-5, 5)) |
| 303 |
executor_manager_thread = executor._executor_manager_thread |
| 304 |
processes = executor._processes |
| 305 |
call_queue = executor._call_queue |
| 306 |
executor_manager_thread = executor._executor_manager_thread |
| 307 |
del executor |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
start = time.monotonic() |
| 314 |
while executor_manager_thread.is_alive(): |
| 315 |
support.gc_collect() |
| 316 |
if time.monotonic() - start > 5: |
| 317 |
assert executor_manager_thread.executor_reference() is None |
| 318 |
executor_manager_thread.join(0.01) |
| 319 |
for p in processes.values(): |
| 320 |
p.join() |
| 321 |
call_queue.join_thread() |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
assert all([r == abs(v) for r, v in zip(res, range(-5, 5))]) |
| 326 |
|
| 327 |
def test_shutdown_no_wait(self): |
| 328 |
|
| 329 |
|
| 330 |
executor = futures.ProcessPoolExecutor( |
| 331 |
max_workers=5, mp_context=self.get_context()) |
| 332 |
res = executor.map(abs, range(-5, 5)) |
| 333 |
processes = executor._processes |
| 334 |
call_queue = executor._call_queue |
| 335 |
executor_manager_thread = executor._executor_manager_thread |
| 336 |
executor.shutdown(wait=False) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
executor_manager_thread.join() |
| 341 |
for p in processes.values(): |
| 342 |
p.join() |
| 343 |
call_queue.join_thread() |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
assert all([r == abs(v) for r, v in zip(res, range(-5, 5))]) |
| 348 |
|
| 349 |
|
| 350 |
create_executor_tests(globals(), ProcessPoolShutdownTest, |
| 351 |
executor_mixins=(ProcessPoolForkMixin, |
| 352 |
ProcessPoolForkserverMixin, |
| 353 |
ProcessPoolSpawnMixin)) |
| 354 |
|
| 355 |
|
| 356 |
def setUpModule(): |
| 357 |
setup_module() |
| 358 |
|
| 359 |
|
| 360 |
if __name__ == "__main__": |
| 361 |
unittest.main() |