VSCode Python Remote Debugging Suddenly Not Working

Question:

Starting a couple days ago my normal process for debugging python code via pytest has just stopped working.

My previous process was as follows:

  1. Insert the debugpy macro either above the pytest test definition or at the top level of the code being debugged.
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
  1. Insert the debugpy.breakpoint() call above the line to stop at OR use the red gutter button to mark a breakpoint (less reliable).

  2. Hit the F5 key when pytest started and the collecting... message appeared in the terminal.

This process worked for the entire two months since I converted from PyCharm. As of a couple days ago, the new behavior is as follows:

  • If I use the debugpy.breakpoint() call to stop the run, the code will stop however in a seemingly random place. Before I reinstalled nearly everything (VSCode, created a new linux VM), it would stop at a random line inside a file from the pytest library. After I reinstalled everything, it now stops a random line inside our test database teardown code.
  • If I use the red gutter button to mark a breakpoint, the debugger will not stop at all.

Things I’ve already tried:

  1. Clean reinstall of VSCode.
  2. Destroy and recreate vagrant VM.
  3. Add "env": {"PYTEST_ADDOPTS": "--no-cov"} to my launch.json. (The linter warns that this property is not allowed here and the option is not added at runtime).
  4. Downgraded the Python extension.
  5. Toggled the justMyCode launch option.
  6. Tried multiple git branches.
  7. Tried multiple modules in our monorepo.

Again, I’d like to emphasize that this did work previously and that I made no configuration changes to cause this.

More info:

Version: 1.74.2 (Universal)
Commit: e8a3071ea4344d9d48ef8a4df2c097372b0c5161
Date: 2022-12-20T10:26:09.430Z (3 wks ago)
Electron: 19.1.8
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Darwin x64 21.6.0
Sandboxed: No

launch.json

    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "/mnt/the_repo_folder",
                    "remoteRoot": "/mnt/the_repo_folder"
                }
            ],
            "justMyCode": true
        }
    ]
}
Asked By: smallpants

||

Answers:

As per the comment by @JialeDu, downgrading debugpy seems to have solved the issue. Turns out the sudden problems coincide with a version release of this package to 1.6.5. As such, downgrading to 1.6.4 from two months ago has solved it. I will be reporting this as bug on the library’s github.

UPDATE – 24 Jan 2023:
The behavior has been hit or miss since I downgraded the package so it seems like that is either not the fix at all or I was just briefly lucky. After more tinkering, I found that the more permanent fix is to disable capturing in pytest by running the test with the flags: -s and --no-cov. Only in certain modules do I have to do this though, which is still strange.

Answered By: smallpants