summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/windows_events.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-01-26 14:04:03 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-01-26 14:04:03 (GMT)
commite0fd157ba0cc92e435e7520b4ff641ca68d72244 (patch)
tree413608ff4152b0b250869964ccb9dd0280cacea8 /Lib/asyncio/windows_events.py
parent2a3f38fd299c09be89a7872ccd15c4aedd5fc145 (diff)
downloadcpython-e0fd157ba0cc92e435e7520b4ff641ca68d72244.zip
cpython-e0fd157ba0cc92e435e7520b4ff641ca68d72244.tar.gz
cpython-e0fd157ba0cc92e435e7520b4ff641ca68d72244.tar.bz2
Issue #23293, asyncio: Rewrite IocpProactor.connect_pipe() as a coroutine
Use a coroutine with asyncio.sleep() instead of call_later() to ensure that the schedule call is cancelled. Add also a unit test cancelling connect_pipe().
Diffstat (limited to 'Lib/asyncio/windows_events.py')
-rw-r--r--Lib/asyncio/windows_events.py39
1 files changed, 18 insertions, 21 deletions
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
index 109f5d3..c9ba785 100644
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -518,28 +518,25 @@ class IocpProactor:
return self._register(ov, pipe, finish_accept_pipe)
- def _connect_pipe(self, fut, address, delay):
- # Unfortunately there is no way to do an overlapped connect to a pipe.
- # Call CreateFile() in a loop until it doesn't fail with
- # ERROR_PIPE_BUSY
- try:
- handle = _overlapped.ConnectPipe(address)
- except OSError as exc:
- if exc.winerror == _overlapped.ERROR_PIPE_BUSY:
- # Polling: retry later
- delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY)
- self._loop.call_later(delay,
- self._connect_pipe, fut, address, delay)
- else:
- fut.set_exception(exc)
- else:
- pipe = windows_utils.PipeHandle(handle)
- fut.set_result(pipe)
-
+ @coroutine
def connect_pipe(self, address):
- fut = futures.Future(loop=self._loop)
- self._connect_pipe(fut, address, CONNECT_PIPE_INIT_DELAY)
- return fut
+ delay = CONNECT_PIPE_INIT_DELAY
+ while True:
+ # Unfortunately there is no way to do an overlapped connect to a pipe.
+ # Call CreateFile() in a loop until it doesn't fail with
+ # ERROR_PIPE_BUSY
+ try:
+ handle = _overlapped.ConnectPipe(address)
+ break
+ except OSError as exc:
+ if exc.winerror != _overlapped.ERROR_PIPE_BUSY:
+ raise
+
+ # ConnectPipe() failed with ERROR_PIPE_BUSY: retry later
+ delay = min(delay * 2, CONNECT_PIPE_MAX_DELAY)
+ yield from tasks.sleep(delay, loop=self._loop)
+
+ return windows_utils.PipeHandle(handle)
def wait_for_handle(self, handle, timeout=None):
"""Wait for a handle.