Skip to content
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

Breakpoints not working with QThread/PySide2 #304

Closed
intrinseca opened this issue Jun 22, 2020 · 5 comments
Closed

Breakpoints not working with QThread/PySide2 #304

intrinseca opened this issue Jun 22, 2020 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@intrinseca
Copy link

Environment data

  • debugpy version: 1.0.0b11
  • OS and version: Windows 10 1909 Build 18363.900
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.5 64-bit
  • Using VS Code or Visual Studio: VS Code

First detected with general release of Python extension and also reproduced with the Insiders edition.

Actual behavior

Breakpoints in an object moved to a new QThread are not triggered

Expected behavior

Breakpoints should work as normal.

I tried to work out if the Qt monkey-patching was being triggered properly, but debugging a debugger was beyond me! Is there any way to check if this has happened?

Steps to reproduce:

  1. pip install PySide2
  2. Debug the following script, with a breakpoint set on the print inside Receiver.test (L11)
import sys
import threading

from PySide2.QtCore import QObject, QThread, Slot
from PySide2.QtWidgets import QApplication, QPushButton


class Receiver(QObject):
    @Slot()
    def test(self):
        print(f"Received Event on thread {threading.current_thread().name}")


if __name__ == "__main__":
    app = QApplication(sys.argv)

    button = QPushButton("Hello World!")
    button.show()

    thread = QThread()
    receiver = Receiver()

    receiver.moveToThread(thread)
    thread.start()

    button.clicked.connect(receiver.test)

    app.exec_()
  1. Click the button on the GUI window
  2. Breakpoint is not triggered

VS Code launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}
@intrinseca
Copy link
Author

The debugger side of things seems to be working - if I manually enable debugpy on the background thread it works as intended:

import sys
import threading

from PySide2.QtCore import QObject, QThread, Slot
from PySide2.QtWidgets import QApplication, QPushButton


class MyQThread(QThread):
    def run(self, *args, **kwargs):
        import debugpy
        debugpy.debug_this_thread()
        super().run()


class Receiver(QObject):
    @Slot()
    def test(self):
        print(f"Received Event on thread {threading.current_thread().name}")


if __name__ == "__main__":
    app = QApplication(sys.argv)

    button = QPushButton("Hello World!")
    button.show()

    thread = MyQThread()
    receiver = Receiver()

    receiver.moveToThread(thread)
    thread.start()

    button.clicked.connect(receiver.test)

    app.exec_()

@int19h
Copy link
Contributor

int19h commented Jun 23, 2020

Qt monkey-patching is supposed to be enabled by default, but it looks like we regressed that.

@int19h int19h self-assigned this Jun 23, 2020
@int19h int19h added the bug Something isn't working label Jun 23, 2020
@int19h
Copy link
Contributor

int19h commented Jun 24, 2020

Looks like we regressed it quite a while ago, actually - before ptvsd became debugpy, even. Since it's not an easy revert, I'm going to do this properly - as a configurable property exposing the entire gamut of relevant pydevd settings, to allow fine tuning. The default will still be "auto" though, matching ptvsd 4 behavior.

int19h added a commit to int19h/debugpy that referenced this issue Jun 24, 2020
Expose pydevd.enable_qt_support() in debugpy.server via debugpy.configure(qt=...) and --configure-qt ...

Expose the same in debugpy.adapter via "qt" debug configuration property.

Default to "auto" for all of the above, and also provide "none" to disable Qt monkey-patching entirely.
@int19h int19h closed this as completed in e8bd6c1 Jun 25, 2020
@msc509
Copy link

msc509 commented Dec 29, 2020

Can you elaborate whether this is fixed? I have the same issue on the latest python extension, failing to capture breakpoints in a QThread. The fix reference above seem to add a command option, but I don't see how to use it. (Or rather, how to use it with VSCode launch options with the Python extension).

@fabioz
Copy link
Collaborator

fabioz commented Jan 2, 2021

This should be fixed...

There are 2 modes of operation, one in which the debugger can debug qt out of the box (when you use Python 3.6 onwards and its compiled extensions are available) and the other which requires some monkey-patching from the debugger.

Given that it's not working for you, you're probably on the 2nd case.

So, to turn on that monkey-patching, please set:

"qt": "auto", in the launch config.

-- note: it seems vscode-python currently complains that Property qt is not allowed., that's ok, this is just that vscode-python still hasn't added that to the schema (it's tracking this at: microsoft/vscode-python#12535).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants