8000 gh-132975: Improve Remote PDB interrupt handling by godlygeek · Pull Request #133223 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132975: Improve Remote PDB interrupt handling #133223

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

Merged
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
Prev Previous commit
Next Next commit
Use ExitStack to reduce nesting in attach
  • Loading branch information
godlygeek committed May 2, 2025
commit 4c0b431a25bcd902b7bf5316ba7474e591d6f9ef
69 changes: 40 additions & 29 deletions Lib/pdb.py
< 6947 tr data-hunk="9eb5d31ebaea0e36a693b8697f5be58782061df8cf04772380a357488573fdd6" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import selectors
import _colorize

from contextlib import ExitStack
from contextlib import closing
from contextlib import contextmanager
from rlcompleter import Completer
Expand Down Expand Up @@ -3250,40 +3251,50 @@ def _connect(host, port, frame, commands, version):

def attach(pid, commands=()):
"""Attach to a running process with the given PID."""
with closing(socket.create_server(("localhost", 0))) as server:
with ExitStack() as stack:
server = stack.enter_context(
closing(socket.create_server(("localhost", 0)))
)

port = server.getsockname()[1]

with tempfile.NamedTemporaryFile("w", delete_on_close=False) as connect_script:
connect_script.write(
textwrap.dedent(
f"""
import pdb, sys
pdb._connect(
host="localhost",
port={port},
frame=sys._getframe(1),
commands={json.dumps("\n".join(commands))},
version={_PdbServer.protocol_version()},
)
"""
connect_script = stack.enter_context(
tempfile.NamedTemporaryFile("w", delete_on_close=False)
)

connect_script.write(
textwrap.dedent(
f"""
import pdb, sys
pdb._connect(
host="localhost",
port={port},
frame=sys._getframe(1),
commands={json.dumps("\n".join(commands))},
version={_PdbServer.protocol_version()},
)
"""
)
connect_script.close()
sys.remote_exec(pid, connect_script.name)

# TODO Add a timeout? Or don't bother since the user can ^C?
client_sock, _ = server.accept()

with closing(client_sock):
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as interrupt_script:
interrupt_script.write(
'import pdb, sys\n'
'if inst := pdb.Pdb._last_pdb_instance:\n'
' inst.set_trace(sys._getframe(1))\n'
)
interrupt_script.close()
)
connect_script.close()
sys.remote_exec(pid, connect_script.name)

# TODO Add a timeout? Or don't bother since the user can ^C?
client_sock, _ = server.accept()

stack.enter_context(closing(client_sock))

interrupt_script = stack.enter_context(
tempfile.NamedTemporaryFile("w", delete_on_close=False)
)
interrupt_script.write(
'import pdb, sys\n'
'if inst := pdb.Pdb._last_pdb_instance:\n'
' inst.set_trace(sys._getframe(1))\n'
)
interrupt_script.close()

_PdbClient(pid, client_sock, interrupt_script.name).cmdloop()
_PdbClient(pid, client_sock, interrupt_script.name).cmdloop()


# Post-Mortem interface
Expand Down
0