Commit 842ff9c2 authored by Anael Beutot's avatar Anael Beutot
Browse files

Handles SIGCHLD for remote shell

parent 13fc5780
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -90,6 +90,7 @@ class RemoteShell(object):
        self.proto = conn.create_tunnel(endpoint=self.endpoint,
                                        on_shutdown=self.on_tunnel_shutdown)
        self.conn = conn
        self.child_watcher = None
        try:
            self.process = subprocess.Popen(
                [exec_], stdout=self.slave,
@@ -104,6 +105,9 @@ class RemoteShell(object):
            # close opened fds
            self.close_cb()
            raise
        self.child_watcher = conn.loop.child(self.process.pid, False,
                                             self.child_cb)
        self.child_watcher.start()

    @property
    def label(self):
@@ -121,11 +125,18 @@ class RemoteShell(object):
        self.close()
        logger.debug('Tunnel closed by sjRPC')

    def child_cb(self, watcher, revents):
        logger.debug('Tunnel closed by process termination')
        self.process.returncode = watcher.rstatus
        self.close()

    def close(self):
        if self.process.poll() is None:
        if self.child_watcher is not None:
            self.child_watcher.stop()
            self.child_watcher = None
        if self.process.returncode is None:
            # process is still alive
            self.process.kill()
            self.process.wait()
        if self.master is not None:
            try:
                os.close(self.master)