Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8d89bf4
Only allow KeyboardInterrupt at specific times
godlygeek Apr 30, 2025
f65f99f
Have _PdbClient work with the socket directly
godlygeek Apr 30, 2025
ec9d3bf
Allow interrupting socket reads on Windows
godlygeek Apr 30, 2025
ed664b8
Use a SIGINT to interrupt the remote on Unix
godlygeek Apr 30, 2025
aafa48c
Handle a ValueError for operations on a closed file
godlygeek May 1, 2025
42e7cec
Use a single complex signal handler for the PDB client
godlygeek May 1, 2025
02da647
Add a news entry
godlygeek May 1, 2025
5abd63a
Update the comment to explain why we catch two exception types
godlygeek May 2, 2025
c9c5bf2
Swap signal_read/signal_write resetting to the outer finally block
godlygeek May 2, 2025
8fa88a5
Use os.kill() on every platform but Windows
godlygeek May 2, 2025
4c0b431
Use `ExitStack` to reduce nesting in `attach`
godlygeek May 2, 2025
5993e06
Use a thread to manage interrupts on Windows
godlygeek May 2, 2025
dd7c9b1
Use the signal handling thread approach on all platforms
godlygeek May 4, 2025
e2391b0
Make all _connect arguments keyword-only
godlygeek May 4, 2025
edd7517
Merge remote-tracking branch 'upstream/main' into improve_remote_pdb_…
godlygeek May 4, 2025
14aa8e7
One line for contextlib imports
godlygeek May 4, 2025
b26ed5c
Add some tests for handling SIGINT in the PDB client
godlygeek May 4, 2025
a860087
Switch back to os.kill() on Unix
godlygeek May 4, 2025
e1bb1d3
Wrap input() calls in a function
godlygeek May 4, 2025
2a7807c
Merge branch 'main' into improve_remote_pdb_interrupt_handling
pablogsal May 5, 2025
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
Next Next commit
Only allow KeyboardInterrupt at specific times
Because our goal is to proxy interrupt signals to the remote process,
it's important that we only accept those signals at moments when we're
ready to handle them and send them on. In particular, we're prepared to
handle them when reading user input for a prompt, and when waiting for
the server to send a new prompt after our last command. We do not want
to accept them at other times, like while we're in the middle of
printing output to the screen, as otherwise a

    while True: "Hello"

executed at the PDB REPL can't reliably be Ctrl-C'd: it's more likely
that the signal will arrive while the client is printing than while it's
waiting for the next line from the server, and if so nothing will be
prepared to handle the `KeyboardInterrupt` and the client will exit.
  • Loading branch information
godlygeek committed Apr 30, 2025
commit 8d89bf4bfd57bca5e86eed541f4bce998668ae81
53 changes: 48 additions & 5 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3009,12 +3009,53 @@ def readline_completion(self, completer):
finally:
readline.set_completer(old_completer)

if not hasattr(signal, "pthread_sigmask"):
# On Windows, we must drop signals arriving while we're ignoring them.
@contextmanager
def _block_sigint(self):
old_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
try:
yield
finally:
signal.signal(signal.SIGINT, old_handler)

@contextmanager
def _handle_sigint(self, handler):
old_handler = signal.signal(signal.SIGINT, handler)
try:
yield
finally:
signal.signal(signal.SIGINT, old_handler)
else:
# On Unix, we can save them to be processed later.
@contextmanager
def _block_sigint(self):
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT})
try:
yield
finally:
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGINT})

@contextmanager
def _handle_sigint(self, handler):
old_handler = signal.signal(signal.SIGINT, handler)
try:
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGINT})
yield
finally:
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT})
signal.signal(signal.SIGINT, old_handler)

def cmdloop(self):
with self.readline_completion(self.complete):
with (
self._block_sigint(),
self.readline_completion(self.complete),
):
while not self.write_failed:
try:
if not (payload_bytes := self.sockfile.readline()):
break
with self._handle_sigint(signal.default_int_handler):
if not (payload_bytes := self.sockfile.readline()):
break
except KeyboardInterrupt:
self.send_interrupt()
continue
Expand Down Expand Up @@ -3061,7 +3102,8 @@ def process_payload(self, payload):
def prompt_for_reply(self, prompt):
while True:
try:
payload = {"reply": self.read_command(prompt)}
with self._handle_sigint(signal.default_int_handler):
payload = {"reply": self.read_command(prompt)}
except EOFError:
payload = {"signal": "EOF"}
except KeyboardInterrupt:
Expand Down Expand Up @@ -3101,7 +3143,8 @@ def complete(self, text, state):
if self.write_failed:
return None

payload = self.sockfile.readline()
with self._handle_sigint(signal.default_int_handler):
payload = self.sockfile.readline()
if not payload:
return None

Expand Down