Skip to content

Commit

Permalink
[core/process] Fix fd-restoring order in FdState.Pop
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 21, 2020
1 parent 10136ce commit 17e2f05
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions core/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,17 @@ class _FdFrame(object):
def __init__(self):
# type: () -> None
self.saved = [] # type: List[Tuple[int, int]]
self.need_close = [] # type: List[int]
self.need_wait = [] # type: List[Tuple[Process, Waiter]]

def Forget(self):
# type: () -> None
"""For exec 1>&2."""
del self.saved[:] # like list.clear() in Python 3.3
del self.need_close[:]
del self.need_wait[:]

def __repr__(self):
# type: () -> str
return '<_FdFrame %s %s>' % (self.saved, self.need_close)
return '<_FdFrame %s>' % self.saved


class FdState(object):
Expand Down Expand Up @@ -271,7 +269,7 @@ def _PushMoveFd(self, fd1, fd2):

def _PushClose(self, fd):
# type: (int) -> None
self.cur_frame.need_close.append(fd)
self.cur_frame.saved.append((-1, fd))

def _PushWait(self, proc, waiter):
# type: (Process, Waiter) -> None
Expand Down Expand Up @@ -464,23 +462,23 @@ def Pop(self):
frame = self.stack.pop()
#log('< Pop %s', frame)
for saved, orig in reversed(frame.saved):
try:
posix.dup2(saved, orig)
except OSError as e:
log('dup2(%d, %d) error: %s', saved, orig, e)
#log('fd state:')
#posix.system('ls -l /proc/%s/fd' % posix.getpid())
raise
posix.close(saved)
#log('dup2 %s %s', saved, orig)

for fd in frame.need_close:
#log('Close %d', fd)
try:
posix.close(fd)
except OSError as e:
log('Error closing descriptor %d: %s', fd, e)
raise
if saved == -1:
#log('Close %d', orig)
try:
posix.close(orig)
except OSError as e:
log('Error closing descriptor %d: %s', orig, e)
raise
else:
try:
posix.dup2(saved, orig)
except OSError as e:
log('dup2(%d, %d) error: %s', saved, orig, e)
#log('fd state:')
#posix.system('ls -l /proc/%s/fd' % posix.getpid())
raise
posix.close(saved)
#log('dup2 %s %s', saved, orig)

# Wait for here doc processes to finish.
for proc, waiter in frame.need_wait:
Expand Down

0 comments on commit 17e2f05

Please sign in to comment.