Skip to content
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
15 changes: 8 additions & 7 deletions Lib/asyncio/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ def pipe_connection_lost(self, fd, exc):
if pipe is not None:
pipe.close()
self.connection_lost(exc)
if exc is None:
self._stdin_closed.set_result(None)
else:
self._stdin_closed.set_exception(exc)
# Since calling `wait_closed()` is not mandatory,
# we shouldn't log the traceback if this is not awaited.
self._stdin_closed._log_traceback = False
if not self._stdin_closed.done():
if exc is None:
self._stdin_closed.set_result(None)
else:
self._stdin_closed.set_exception(exc)
# Since calling `wait_closed()` is not mandatory,
# we shouldn't log the traceback if this is not awaited.
self._stdin_closed._log_traceback = False
return
if fd == 1:
reader = self.stdout
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_asyncio/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,31 @@ async def write_stdin(proc, data):
self.loop.run_until_complete, coro)
self.loop.run_until_complete(proc.wait())

def test_stdin_wait_closed_cancelled(self):
# gh-162217: cancelling a task waiting in stdin.wait_closed() must
# not make pipe_connection_lost() fail with InvalidStateError.
async def main():
proc = await asyncio.create_subprocess_exec(
*PROGRAM_CAT,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
)
try:
task = asyncio.create_task(proc.stdin.wait_closed())
await asyncio.sleep(0)
task.cancel()
with self.assertRaises(asyncio.CancelledError):
await task
finally:
proc.stdin.close()
await proc.wait()

messages = []
self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))

self.loop.run_until_complete(main())
self.assertEqual(messages, [])

def test_communicate_ignore_broken_pipe(self):
# buffer large enough to feed the whole pipe buffer
large_data = b'x' * support.PIPE_MAX_SIZE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :exc:`asyncio.InvalidStateError` being logged when the standard input of
an :mod:`asyncio` subprocess is closed after a task waiting in
:meth:`proc.stdin.wait_closed() <asyncio.StreamWriter.wait_closed>` was
cancelled.
Loading