Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GH-100573: Fix server hang caused by os.stat() on named pipe (Windows) #100959

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 6 commits into from
Jan 13, 2023
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
5 changes: 5 additions & 0 deletions Lib/asyncio/windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ def loop_accept_pipe(f=None):
return

f = self._proactor.accept_pipe(pipe)
except BrokenPipeError:
if pipe and pipe.fileno() != -1:
pipe.close()
self.call_soon(loop_accept_pipe)
except OSError as exc:
if pipe and pipe.fileno() != -1:
self.call_exception_handler({
Expand All @@ -377,6 +381,7 @@ def loop_accept_pipe(f=None):
elif self._debug:
logger.warning("Accept pipe failed on pipe %r",
pipe, exc_info=True)
self.call_soon(loop_accept_pipe)
except exceptions.CancelledError:
if pipe:
pipe.close()
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_asyncio/test_windows_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,46 @@ def test_address_argument_type_error(self):
proactor.sendto(sock, b'abc', addr=bad_address)
sock.close()

def test_client_pipe_stat(self):
res = self.loop.run_until_complete(self._test_client_pipe_stat())
self.assertEqual(res, 'done')

async def _test_client_pipe_stat(self):
# Regression test for https://github.com/python/cpython/issues/100573
ADDRESS = r'\\.\pipe\test_client_pipe_stat-%s' % os.getpid()

async def probe():
# See https://github.com/python/cpython/pull/100959#discussion_r1068533658
h = _overlapped.ConnectPipe(ADDRESS)
try:
_winapi.CloseHandle(_overlapped.ConnectPipe(ADDRESS))
except OSError as e:
if e.winerror != _overlapped.ERROR_PIPE_BUSY:
raise
finally:
_winapi.CloseHandle(h)

with self.assertRaises(FileNotFoundError):
await probe()

[server] = await self.loop.start_serving_pipe(asyncio.Protocol, ADDRESS)
self.assertIsInstance(server, windows_events.PipeServer)

errors = []
self.loop.set_exception_handler(lambda _, data: errors.append(data))

for i in range(5):
await self.loop.create_task(probe())

self.assertEqual(len(errors), 0, errors)

server.close()

with self.assertRaises(FileNotFoundError):
await probe()

return "done"


class WinPolicyTests(test_utils.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a Windows :mod:`asyncio` bug with named pipes where a client doing ``os.stat()`` on the pipe would cause an error in the server that disabled serving future requests.