Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions deadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ def submit(
return fut

def shutdown(self, wait: bool = True, *, cancel_futures: bool = False) -> None:
if self.closed:
return

logger.debug(f"shutdown: {wait=} {cancel_futures=}")

# No more new tasks can be submitted
Expand Down Expand Up @@ -706,16 +709,14 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if not self.closed:
kwargs = {}
if self.shutdown_wait is not None:
kwargs["wait"] = self.shutdown_wait

if self.shutdown_cancel_futures is not None:
kwargs["cancel_futures"] = self.shutdown_cancel_futures
kwargs = {}
if self.shutdown_wait is not None:
kwargs["wait"] = self.shutdown_wait

self.shutdown(**kwargs)
if self.shutdown_cancel_futures is not None:
kwargs["cancel_futures"] = self.shutdown_cancel_futures

self.shutdown(**kwargs)
self.runner_thread.join()
return False

Expand Down
8 changes: 8 additions & 0 deletions tests/test_deadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ def test_shutdown(logging_initializer, wait, cancel_futures):
assert result == 123


@pytest.mark.parametrize("wait", [True, False])
def test_shutdown_idempotent(wait):
"""Calling shutdown() twice must not deadlock."""
d = deadpool.Deadpool(max_workers=2)
d.shutdown(wait=wait)
d.shutdown(wait=wait)


@pytest.mark.parametrize("wait", [True, False])
@pytest.mark.parametrize("cancel_futures", [True, False])
def test_shutdown_manual(logging_initializer, wait, cancel_futures):
Expand Down
Loading