Skip to content

Commit

Permalink
Improve handling of closed debug processes
Browse files Browse the repository at this point in the history
Currently the debug processes can be closed but the user may not be
notified. This can lead to them submitting commands that don't do
anything with no feedback.

These changes to `mdb/mdb_client.py` and `mdb/mdb_shell.py` provide a
warning when all debug processes `client.dbg_procs` are closed.

This may also paves the way for handling debug session restarts (pending
a re-write of the mdb launcher).
  • Loading branch information
TomMelt committed Jan 12, 2024
1 parent ff41651 commit d458ce1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
24 changes: 13 additions & 11 deletions mdb/mdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ def __init__(self, prog_opts: Prog_opts) -> None:
def close_procs(self) -> None:
"""Close all open processes."""

with Progress(
TextColumn("[progress.description]{task.description}"),
BarColumn(),
MofNCompleteColumn(),
) as progress:
connect_progress = progress.add_task(
"Closing processes...", total=self.ranks
)
for proc in self.dbg_procs:
proc.close()
progress.advance(connect_progress)
if self.dbg_procs:
with Progress(
TextColumn("[progress.description]{task.description}"),
BarColumn(),
MofNCompleteColumn(),
) as progress:
connect_progress = progress.add_task(
"Closing processes...", total=self.ranks
)
for proc in self.dbg_procs:
proc.close()
progress.advance(connect_progress)
self.dbg_procs = []
return

def clear_stdout(self) -> None:
Expand Down
27 changes: 23 additions & 4 deletions mdb/mdb_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import matplotlib.pyplot as plt
import numpy as np
import pexpect # type: ignore

from .utils import (
parse_ranks,
Expand All @@ -26,7 +27,7 @@
)

if TYPE_CHECKING:
from pexpect import spawn # type: ignore
from pexpect import spawn

from .mdb_attach import Prog_opts
from .mdb_client import Client
Expand Down Expand Up @@ -237,7 +238,12 @@ def send_command(command: str, rank: int) -> None:
select = parse_ranks(commands[0])
command = " ".join(commands[1:])

self.client.pool.starmap(send_command, zip(itertools.repeat(command), select))
try:
self.client.pool.starmap(
send_command, zip(itertools.repeat(command), select)
)
except pexpect.EOF:
self.client.close_procs()

return

Expand Down Expand Up @@ -384,8 +390,16 @@ def status_to_color(rank: int, at_breakpoint: bool) -> str:

return

def precmd(self, line: str) -> str:
"""Override Cmd.precmd() to only run the command if debug processes are open."""
if self.client.dbg_procs or line in ["quit", "EOF"]:
return line
else:
print("warning: no debug processes running. Please relaunch the debugger")
return "NULL"

def preloop(self) -> None:
"""Override cmd preloop method to load mdb history."""
"""Override Cmd.preloop() to load mdb history."""

readline.parse_and_bind('"\\e[A": history-search-backward')
readline.parse_and_bind('"\\e[B": history-search-forward')
Expand All @@ -396,7 +410,7 @@ def preloop(self) -> None:
return

def postloop(self) -> None:
"""Override cmd postloop method to save mdb history and close gdb processes."""
"""Override Cmd.postloop() to save mdb history and close gdb processes."""

readline.set_history_length(self.hist_filesize)
readline.write_history_file(self.hist_file)
Expand Down Expand Up @@ -429,8 +443,13 @@ def send_interrupt(rank: int) -> None:
def default(self, line: str) -> bool: # type: ignore[override]
"""Method called on an input line when the command prefix is not recognized."""
if line == "EOF":
# Cmd converts CTRL+D to "EOF"
self.onecmd("quit")
return True
elif line == "NULL":
# do nothing
# useful to have a fake command for precmd()
return False
else:
print(
f"unrecognized command [{line}]. Type help to find out list of possible commands."
Expand Down

0 comments on commit d458ce1

Please sign in to comment.