Skip to content

Commit

Permalink
Sharing file descriptors causes confusion; DMOJ#229
Browse files Browse the repository at this point in the history
  • Loading branch information
quantum5 committed Dec 19, 2016
1 parent d4a7631 commit a24d03e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dmoj/cptbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def __init_streams(self, stdin, stdout, stderr, unbuffered):
master, slave = pty.openpty()

if stdin is PIPE:
self._child_stdin, self._stdin = (slave, master) if unbuffered else os.pipe()
self._child_stdin, self._stdin = (os.dup(slave), os.dup(master)) if unbuffered else os.pipe()
self.stdin = os.fdopen(self._stdin, 'w')
elif isinstance(stdin, int):
self._child_stdin, self._stdin = stdin, -1
Expand All @@ -302,7 +302,7 @@ def __init_streams(self, stdin, stdout, stderr, unbuffered):
self._child_stdin = self._stdin = -1

if stdout is PIPE:
self._stdout, self._child_stdout = (master, slave) if unbuffered else os.pipe()
self._stdout, self._child_stdout = (os.dup(master), os.dup(slave)) if unbuffered else os.pipe()
self.stdout = os.fdopen(self._stdout, 'r')
elif isinstance(stdout, int):
self._stdout, self._child_stdout = -1, stdout
Expand All @@ -321,6 +321,10 @@ def __init_streams(self, stdin, stdout, stderr, unbuffered):
else:
self._stderr = self._child_stderr = -1

if unbuffered:
os.close(master)
os.close(slave)

# All communicate stuff copied from subprocess.
def communicate(self, input=None):
# Optimization: If we are only using one pipe, or no pipe at
Expand Down

0 comments on commit a24d03e

Please sign in to comment.