Skip to content
Open
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
10 changes: 6 additions & 4 deletions Lib/asyncio/windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,13 @@ def _poll(self, timeout=None):
try:
value = callback(transferred, key, ov)
except OSError as e:
f.set_exception(e)
self._results.append(f)
if not f.done():
f.set_exception(e)
self._results.append(f)
else:
f.set_result(value)
self._results.append(f)
if not f.done():
f.set_result(value)
self._results.append(f)
finally:
f = None

Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_asyncio/test_windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,38 @@ def test_wait_for_handle_cancel(self):
fut.cancel()
fut.cancel()

def _poll_with_future_done_during_callback(self, callback_exception=None):
proactor = self.loop._proactor
ov = _overlapped.Overlapped(_winapi.NULL)
fut = windows_events._OverlappedFuture(ov, loop=self.loop)

class Obj:
pass

def callback(transferred, key, ov):
self.assertFalse(fut.done())
fut.cancel()
if callback_exception is not None:
raise callback_exception
return object()

proactor._cache[ov.address] = (fut, ov, Obj(), callback)
_overlapped.PostQueuedCompletionStatus(proactor._iocp, 0, 0,
ov.address)

proactor._poll(0)

self.assertTrue(fut.cancelled())
self.assertEqual(proactor._results, [])

def test_poll_skips_exception_if_future_done_during_callback(self):
exc = ConnectionResetError(_overlapped.ERROR_OPERATION_ABORTED,
"operation aborted")
self._poll_with_future_done_during_callback(exc)

def test_poll_skips_result_if_future_done_during_callback(self):
self._poll_with_future_done_during_callback()

def test_read_self_pipe_restart(self):
# Regression test for https://bugs.python.org/issue39010
# Previously, restarting a proactor event loop in certain states
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid raising :exc:`asyncio.InvalidStateError` from the Windows proactor event
loop when an overlapped future becomes done while handling an I/O completion.
Loading