diff --git a/pty_session.py b/pty_session.py index 937d843..6d5b9eb 100644 --- a/pty_session.py +++ b/pty_session.py @@ -102,6 +102,15 @@ class _UnixPty: os.kill(self.pid, signal.SIGKILL) except OSError: pass + try: # reap immediately and capture the exit status + wpid, status = os.waitpid(self.pid, 0) + if wpid == self.pid: + if os.WIFEXITED(status): + self._exit_code = os.WEXITSTATUS(status) + elif os.WIFSIGNALED(status): + self._exit_code = -os.WTERMSIG(status) + except OSError: + pass if self.fd is not None: try: os.close(self.fd) diff --git a/tests/test_pty_session.py b/tests/test_pty_session.py index a4105f1..5a37246 100644 --- a/tests/test_pty_session.py +++ b/tests/test_pty_session.py @@ -60,6 +60,7 @@ def test_terminate_stops_session(): while s.is_alive() and time.time() < deadline: time.sleep(0.05) assert s.is_alive() is False + assert s.exit_code() == -9 # killed by SIGKILL def test_resize_applies_to_pty():