Skip to content
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

[3.8] bpo-39010: Fix errors logged on proactor loop restart (GH-22017) #22035

Merged
merged 1 commit into from Sep 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/asyncio/proactor_events.py
Expand Up @@ -766,6 +766,14 @@ def _loop_self_reading(self, f=None):
try:
if f is not None:
f.result() # may raise
if self._self_reading_future is not f:
# When we scheduled this Future, we assigned it to
# _self_reading_future. If it's not there now, something has
# tried to cancel the loop while this callback was still in the
# queue (see windows_events.ProactorEventLoop.run_forever). In
# that case stop here instead of continuing to schedule a new
# iteration.
return
f = self._proactor.recv(self._ssock, 4096)
except exceptions.CancelledError:
# _close_self_pipe() has been called, stop waiting for data
Expand Down
8 changes: 6 additions & 2 deletions Lib/asyncio/windows_events.py
Expand Up @@ -318,8 +318,12 @@ def run_forever(self):
if self._self_reading_future is not None:
ov = self._self_reading_future._ov
self._self_reading_future.cancel()
# self_reading_future was just cancelled so it will never be signalled
# Unregister it otherwise IocpProactor.close will wait for it forever
# self_reading_future was just cancelled so if it hasn't been
# finished yet, it never will be (it's possible that it has
# already finished and its callback is waiting in the queue,
# where it could still happen if the event loop is restarted).
# Unregister it otherwise IocpProactor.close will wait for it
# forever
if ov is not None:
self._proactor._unregister(ov)
self._self_reading_future = None
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_asyncio/test_proactor_events.py
Expand Up @@ -736,6 +736,7 @@ def test_loop_self_reading(self):

def test_loop_self_reading_fut(self):
fut = mock.Mock()
self.loop._self_reading_future = fut
self.loop._loop_self_reading(fut)
self.assertTrue(fut.result.called)
self.proactor.recv.assert_called_with(self.ssock, 4096)
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_asyncio/test_windows_events.py
Expand Up @@ -211,6 +211,26 @@ def test_wait_for_handle_cancel(self):
fut.cancel()
fut.cancel()

def test_read_self_pipe_restart(self):
# Regression test for https://bugs.python.org/issue39010
# Previously, restarting a proactor event loop in certain states
# would lead to spurious ConnectionResetErrors being logged.
self.loop.call_exception_handler = mock.Mock()
# Start an operation in another thread so that the self-pipe is used.
# This is theoretically timing-dependent (the task in the executor
# must complete before our start/stop cycles), but in practice it
# seems to work every time.
f = self.loop.run_in_executor(None, lambda: None)
self.loop.stop()
self.loop.run_forever()
self.loop.stop()
self.loop.run_forever()
# If we don't wait for f to complete here, we may get another
# warning logged about a thread that didn't shut down cleanly.
self.loop.run_until_complete(f)
self.loop.close()
self.assertFalse(self.loop.call_exception_handler.called)


class WinPolicyTests(test_utils.TestCase):

Expand Down
@@ -0,0 +1,2 @@
Restarting a ``ProactorEventLoop`` on Windows no longer logs spurious
``ConnectionResetErrors``.