Skip to content

Commit

Permalink
[DEBUG] Implement proper debugger termination
Browse files Browse the repository at this point in the history
  • Loading branch information
ObaraEmmanuel committed Jan 8, 2025
1 parent fd17ba4 commit 73ed2ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
20 changes: 14 additions & 6 deletions studio/debugtools/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def stream_client(self):
try:
msg = self._stream_client.recv()
except (ConnectionAbortedError, ConnectionResetError, OSError):
self.exit()
self.terminate()
break
if msg == "TERMINATE":
self.exit()
self.terminate()
break
if hasattr(msg, "payload"):
msg.payload = unmarshal(msg.payload, self)
Expand Down Expand Up @@ -206,13 +206,21 @@ def destroy(self) -> None:
super().destroy()

def exit(self):
# Debugger initiated exit
try:
self.transmit("TERMINATE")
self.close_clients()
self.destroy()
except:
pass
finally:
# Exit forcefully
os._exit(os.EX_OK)
self.destroy()

def terminate(self):
# Hook initiated exit
try:
self.close_clients()
except:
pass
self.destroy()

@classmethod
def run_process(cls, path) -> subprocess.Popen:
Expand Down
25 changes: 15 additions & 10 deletions studio/debugtools/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def __init__(self, path=None):
self._server_clients = []
self.selection = []
atexit.register(self.terminate)
# force close preferences
pref._release()

@property
def allow_hover(self):
Expand Down Expand Up @@ -338,27 +340,30 @@ def console_run(self):
def run_command(last_compiled):
try:
self.shell.runcode(last_compiled)
except SystemExit:
self.root.after(0, self.exit)

self.transmit(
Message("CONSOLE", payload={"note": "COMMAND_COMPLETE"})
)
self.transmit(
Message("CONSOLE", payload={"note": "COMMAND_COMPLETE"})
)
except SystemExit as e:
_code = e.code
self.root.after(0, lambda: exit(_code))

threading.Thread(target=run_command, args=(self.last_compiled,)).start()
self.last_compiled = None

def exit(self):
# Debugger initiated termination
self.enable_hooks = False
try:
for client in self._server_clients:
client.close()
except:
pass
self.root.quit()
self.terminate()

def terminate(self):
# Hook initiated termination
self.transmit("TERMINATE")

for client in self._server_clients:
client.close()

def extract_base_class(self, widget):
return extract_base_class(widget)

Expand Down

0 comments on commit 73ed2ad

Please sign in to comment.