Skip to content

Remote debugger stops working when SIGINT(KeyboardInterrupt) is sent while application is suspended #984

@crftwr

Description

@crftwr

Environment data

  • debugpy version: 1.5.1
  • OS and version: macOS Monterey 12.4
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.10
  • Using VS Code or Visual Studio: VS Code

Actual behavior

I would like to handle SIGINT signal (=KeyboardInterrupt exception) in my Python application gracefully, and continue running.

With debugpy, firstly I can debug my Python application, I can handle KeyboardInterrupt exception without problem, when I hit Ctrl-C while application is running (not suspended by debugger).

But when I hit Ctrl-C while application is suspended by debugger (by break-point, or step-execution), the debugger stops working.

I see following message from the debugger thread:

  File "_pydevd_bundle/pydevd_cython.pyx", line 1078, in _pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch
  File "_pydevd_bundle/pydevd_cython.pyx", line 297, in _pydevd_bundle.pydevd_cython.PyDBFrame.do_wait_suspend
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 1976, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 2011, in _do_wait_suspend
    time.sleep(0.01)
KeyboardInterrupt

Once this situation happens, debugpy.is_client_connected() returns True, and debugpy.wait_for_client() returns immediately, but I cannot use debugger capabilities such as step-executions.

Expected behavior

Debugger should work even after hitting Ctrl-C, as far as the target application handles it gracefully.

Steps to reproduce:

  1. Run following script in a Terminal window
  2. Attach remote debugger from VS Code to this process ("localhost", 5678)
  3. Confirm you see "0", "1", "2"... in the "DEBUG CONSOLE" on VS Code.
  4. Click "Pause" button on VS Code, and confirm the application pauses.
  5. Hit Ctrl-C key in the Terminal window.
  6. "Gracefully handling KeyboardInterrupt" is printed, and see "0", "1", "2"... in the "DEBUG CONSOLE" again.
  7. Try to use "Pause" button on VS Code, but it doesn't work.
import time
import debugpy

port = 5678
debugpy.listen( ( "0.0.0.0", port ) )

def mainPortionOfMyApp():
    i = 0
    while True:
        print(i)
        i+=1
        time.sleep(1)

while True:

    try:
        mainPortionOfMyApp()

    except KeyboardInterrupt:
        print("Gracefully handling KeyboardInterrupt")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions