Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with closing the session in remote debugging #450

Merged
merged 4 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pudb/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,16 @@
# {{{ debugger interface

class Debugger(bdb.Bdb):
is_active = True
is_remote = False

def __init__(self, stdin=None, stdout=None, term_size=None, steal_output=False,
**kwargs):

if self.is_remote and not hasattr(self, "close_remote_session"):
raise RuntimeError(
"Remote debuggers need to implement close_remote_session")

# Pass remaining kwargs to python debugger framework
bdb.Bdb.__init__(self, **kwargs)
self.ui = DebuggerUI(self, stdin=stdin, stdout=stdout, term_size=term_size)
Expand Down Expand Up @@ -2371,6 +2379,8 @@ def hide(self):
self.show_count -= 1
if self.show_count == 0:
self.screen.stop()
if self.debugger.is_remote and not self.debugger.is_active:
self.debugger.close_remote_session()

def call_with_ui(self, f, *args, **kwargs):
self.show()
Expand Down
30 changes: 11 additions & 19 deletions pudb/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class RemoteDebugger(Debugger):
me = "pudb"
_prev_outs = None
_sock = None
is_remote = True

def __init__(
self,
Expand All @@ -89,7 +90,6 @@ def __init__(
term_size=None,
reverse=False,
):
self.active = True
self.out = out

self._prev_handles = sys.stdin, sys.stdout
Expand Down Expand Up @@ -178,36 +178,28 @@ def get_avail_port(self, host, port, search_limit=100, skew=+0):
def say(self, m):
print(m, file=self.out)

def _close_session(self):
def close_remote_session(self):
self.stdin, self.stdout = sys.stdin, sys.stdout = self._prev_handles
self._handle.close()
self._client.close()
self._sock.close()
self.active = False
if self._sock:
self._sock.close()
self.say(SESSION_ENDED.format(self=self))

def do_continue(self, arg):
self._close_session()
self.set_continue()
return 1

do_c = do_cont = do_continue

def do_quit(self, arg):
self._close_session()
self.set_quit()
return 1

def set_quit(self):
# this raises a BdbQuit exception that we are unable to catch.
sys.settrace(None)
self.is_active = False
return super().set_quit()

def set_continue(self):
self.is_active = False
return super().set_continue()


def debugger(term_size=None, host=PUDB_RDB_HOST, port=PUDB_RDB_PORT, reverse=False):
"""Return the current debugger instance (if any),
or creates a new one."""
rdb = _current[0]
if rdb is None or not rdb.active:
if rdb is None or not rdb.is_active:
rdb = _current[0] = RemoteDebugger(
host=host, port=port, term_size=term_size, reverse=reverse
)
Expand Down