Skip to content

Commit

Permalink
Get mitogen.fakessh module working again
Browse files Browse the repository at this point in the history
Fixes include

- Setting cloexec flag on pipe files, using set_inheritable on sockets,
  and close_fds=False on subprocess.Popen to work around file
  descriptors not being inheritable by default in new versions of python

- Adding mitogen.exit_status variable and avoiding os.kill call so fake
  'ssh' script is able to exit cleanly with correct status code

- Fixing broken os.dup call in ExternalContext._setup_master when input
  and output streams have the same descriptor

- Updating fakessh module to do necessary python3 string/byte
  conversions, and use updated mitogen Protocol, Stream, and Router apis

- Simplifying fakessh startup sequence so there aren't unnecessary
  differences between ways control and data handles are passed, and ways
  master and slave processes are initialized

- Fixing shutdown race conditions where subprocess exit handling or
  stdin EOF handling could result in a truncated stdout stream

- Updating and adding a lot of docstrings and comments

- Adding Process.proc is None / is not None assertions to be clear about
  which parts of fakessh.Process code are specific to the slave process,
  and which parts are specific to the master process.

- Re-enabling unit test case and updating an outdated file path so it
  passes
  • Loading branch information
ryanofsky committed Jan 19, 2020
1 parent c289b16 commit 56f25e1
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 91 deletions.
12 changes: 7 additions & 5 deletions mitogen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ def set_protocol(self, protocol):
self.protocol = protocol
self.protocol.stream = self

def accept(self, rfp, wfp):
def accept(self, rfp, wfp, cloexec=True):
"""
Attach a pair of file objects to :attr:`receive_side` and
:attr:`transmit_side`, after wrapping them in :class:`Side` instances.
Expand All @@ -1677,8 +1677,8 @@ def accept(self, rfp, wfp):
:param file wfp:
The file object to transmit to.
"""
self.receive_side = Side(self, rfp)
self.transmit_side = Side(self, wfp)
self.receive_side = Side(self, rfp, cloexec=cloexec)
self.transmit_side = Side(self, wfp, cloexec=cloexec)

def __repr__(self):
return "<Stream %s #%04x>" % (self.name, id(self) & 0xffff,)
Expand Down Expand Up @@ -3767,7 +3767,7 @@ def __init__(self, config):
self.config = config

def _on_broker_exit(self):
if not self.config['profiling']:
if not self.config['profiling'] and not hasattr(mitogen, "exit_status"):
os.kill(os.getpid(), signal.SIGTERM)

def _on_shutdown_msg(self, msg):
Expand Down Expand Up @@ -3822,9 +3822,11 @@ def _setup_master(self):

in_fd = self.config.get('in_fd', 100)
in_fp = os.fdopen(os.dup(in_fd), 'rb', 0)
out_fp = os.fdopen(os.dup(self.config.get('out_fd', 1)), 'wb', 0)
# Avoid closing in_fd until after duplicating out_fd in case
# (in_fd == out_fd) are the same bidirectional socket fd
os.close(in_fd)

out_fp = os.fdopen(os.dup(self.config.get('out_fd', 1)), 'wb', 0)
self.stream = MitogenProtocol.build_stream(
self.router,
parent_id,
Expand Down
Loading

0 comments on commit 56f25e1

Please sign in to comment.