Skip to content

Commit

Permalink
Fix delegator leaky file handles
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Ryan <[email protected]>
  • Loading branch information
techalchemy committed Oct 30, 2018
1 parent 4c86172 commit e375eb3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pipenv/vendor/delegator.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def run(self, block=True, binary=False, cwd=None, env=None):
# Use subprocess.
if self.blocking:
popen_kwargs = self._default_popen_kwargs.copy()
del popen_kwargs["stdin"]
popen_kwargs["universal_newlines"] = not binary
if cwd:
popen_kwargs["cwd"] = cwd
Expand Down Expand Up @@ -234,14 +235,22 @@ def block(self):
"""Blocks until process is complete."""
if self._uses_subprocess:
# consume stdout and stderr
try:
stdout, stderr = self.subprocess.communicate()
self.__out = stdout
self.__err = stderr
except ValueError:
pass # Don't read from finished subprocesses.
if self.blocking:
try:
stdout, stderr = self.subprocess.communicate()
self.__out = stdout
self.__err = stderr
except ValueError:
pass # Don't read from finished subprocesses.
else:
self.subprocess.stdin.close()
self.std_out.close()
self.std_err.close()
self.subprocess.wait()
else:
self.subprocess.sendeof()
self.subprocess.wait()
self.subprocess.proc.stdout.close()

def pipe(self, command, timeout=None, cwd=None):
"""Runs the current command and passes its output to the next
Expand Down
44 changes: 44 additions & 0 deletions tasks/vendoring/patches/vendor/delegator-close-filehandles.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
diff --git a/pipenv/vendor/delegator.py b/pipenv/vendor/delegator.py
index 0c140cad..3ffb2e31 100644
--- a/pipenv/vendor/delegator.py
+++ b/pipenv/vendor/delegator.py
@@ -178,6 +178,7 @@ class Command(object):
# Use subprocess.
if self.blocking:
popen_kwargs = self._default_popen_kwargs.copy()
+ del popen_kwargs["stdin"]
popen_kwargs["universal_newlines"] = not binary
if cwd:
popen_kwargs["cwd"] = cwd
@@ -233,18 +234,23 @@ class Command(object):
def block(self):
"""Blocks until process is complete."""
if self._uses_subprocess:
- self.subprocess.stdin.close()
# consume stdout and stderr
- try:
- stdout, stderr = self.subprocess.communicate()
- self.__out = stdout
- self.__err = stderr
- except ValueError:
- pass # Don't read from finished subprocesses.
+ if self.blocking:
+ try:
+ stdout, stderr = self.subprocess.communicate()
+ self.__out = stdout
+ self.__err = stderr
+ except ValueError:
+ pass # Don't read from finished subprocesses.
+ else:
+ self.subprocess.stdin.close()
+ self.std_out.close()
+ self.std_err.close()
+ self.subprocess.wait()
else:
self.subprocess.sendeof()
- self.subprocess.proc.stdout.close()
self.subprocess.wait()
+ self.subprocess.proc.stdout.close()

def pipe(self, command, timeout=None, cwd=None):
"""Runs the current command and passes its output to the next

0 comments on commit e375eb3

Please sign in to comment.