◐ Shell
clean mode source ↗

gh-132975: Improve Remote PDB interrupt handling by godlygeek · Pull Request #133223 · python/cpython

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.

@bedevere-app Bot mentioned this pull request

Apr 30, 2025
Previously we worked with a file object created with `socket.makefile`,
but on Windows the `readline()` method of a socket file can't be
interrupted with Ctrl-C, and neither can `recv()` on a socket. This
refactor lays the ground work for a solution this this problem.
Since a socket read can't be directly interrupted on Windows, use the
`selectors` module to watch for either new data to be read from that
socket, or for a signal to arrive (leveraging `signal.set_wakeup_fd`).
This allows us to watch for both types of events and handle whichever
arrives first.
This unfortunately cannot be applied to Windows, which has no way to
trigger a SIGINT signal in a remote process without running code inside
of that process itself.

@godlygeek godlygeek marked this pull request as ready for review

April 30, 2025 22:21

gaogaotiantian

It seems like this is raised explicitly in some cases.

gaogaotiantian

gaogaotiantian

Let the caller of `_connect` tell us whether to start the thread.
This gives us greater flexibility to reorder things or add things in the
future without breaking backwards compatibility.

gaogaotiantian

gaogaotiantian

@pablogsal

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request

Jul 12, 2025