Skip to content

Commit fbb5546

Browse files
committed
bpo-39744: make asyncio.subprocess communicate similar to non-asyncio one
subprocess's communicate(None) closes stdin of the child process, after sending no (extra) data. Make asyncio variant do the same. This fixes issues with processes that waits for EOF on stdin before continuing.
1 parent 8af4712 commit fbb5546

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Lib/asyncio/subprocess.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,11 @@ def kill(self):
145145

146146
async def _feed_stdin(self, input):
147147
debug = self._loop.get_debug()
148-
self.stdin.write(input)
149-
if debug:
150-
logger.debug(
151-
'%r communicate: feed stdin (%s bytes)', self, len(input))
148+
if input is not None:
149+
self.stdin.write(input)
150+
if debug:
151+
logger.debug(
152+
'%r communicate: feed stdin (%s bytes)', self, len(input))
152153
try:
153154
await self.stdin.drain()
154155
except (BrokenPipeError, ConnectionResetError) as exc:
@@ -181,7 +182,7 @@ async def _read_stream(self, fd):
181182
return output
182183

183184
async def communicate(self, input=None):
184-
if input is not None:
185+
if self.stdin is not None:
185186
stdin = self._feed_stdin(input)
186187
else:
187188
stdin = self._noop()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make func:`asyncio.subprocess.Process.communicate` close subprocess's stdin even when called with input=None

0 commit comments

Comments
 (0)