Skip to content

gh-111358: Fix timeout behaviour in BaseEventLoop.shutdown_default_executor #115622

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 3 commits into from
Feb 19, 2024
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
20 changes: 11 additions & 9 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from . import sslproto
from . import staggered
from . import tasks
from . import timeouts
from . import transports
from . import trsock
from .log import logger
Expand Down Expand Up @@ -598,23 +599,24 @@ async def shutdown_default_executor(self, timeout=None):
thread = threading.Thread(target=self._do_shutdown, args=(future,))
thread.start()
try:
await future
finally:
thread.join(timeout)

if thread.is_alive():
async with timeouts.timeout(timeout):
await future
except TimeoutError:
warnings.warn("The executor did not finishing joining "
f"its threads within {timeout} seconds.",
RuntimeWarning, stacklevel=2)
f"its threads within {timeout} seconds.",
RuntimeWarning, stacklevel=2)
self._default_executor.shutdown(wait=False)
else:
thread.join()

def _do_shutdown(self, future):
try:
self._default_executor.shutdown(wait=True)
if not self.is_closed():
self.call_soon_threadsafe(future.set_result, None)
self.call_soon_threadsafe(futures._set_result_unless_cancelled,
future, None)
except Exception as ex:
if not self.is_closed():
if not self.is_closed() and not future.cancelled():
self.call_soon_threadsafe(future.set_exception, ex)

def _check_running(self):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ def test_set_default_executor_error(self):

self.assertIsNone(self.loop._default_executor)

def test_shutdown_default_executor_timeout(self):
class DummyExecutor(concurrent.futures.ThreadPoolExecutor):
def shutdown(self, wait=True, *, cancel_futures=False):
if wait:
time.sleep(0.1)

self.loop._process_events = mock.Mock()
self.loop._write_to_self = mock.Mock()
executor = DummyExecutor()
self.loop.set_default_executor(executor)

with self.assertWarnsRegex(RuntimeWarning,
"The executor did not finishing joining"):
self.loop.run_until_complete(
self.loop.shutdown_default_executor(timeout=0.01))

def test_call_soon(self):
def cb():
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug in :meth:`asyncio.BaseEventLoop.shutdown_default_executor` to
ensure the timeout passed to the coroutine behaves as expected.