Skip to content

Commit 4bf5647

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 dc3f975 commit 4bf5647

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
@@ -144,10 +144,11 @@ def kill(self):
144144

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

182183
async def communicate(self, input=None):
183-
if input is not None:
184+
if self.stdin is not None:
184185
stdin = self._feed_stdin(input)
185186
else:
186187
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)