3

On my laptop I'm able to attach a VS Code debugger to a running python process, but on my desktop it always times out trying to connect.

  • Laptop: Debian Stable (11) , python 3.9
  • Desktop: Pop OS (Ubuntu 21.10), python 3.9.7
  • I'm signed in to a Github account so the VS Code settings and extensions are synced.

I'm using this simple test file:

import time

while True:
    print("hello, world")
    time.sleep(1)

Debugging works fine when I start the script from (the default) launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    }
  ]
}

But when I use the following (also default) launch.json to attach to my already running process, I always end op getting a timeout message (on the desktop, the same script does work on the laptop):

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Attach using Process Id",
      "type": "python",
      "request": "attach",
      "processId": "${command:pickProcess}",
      "justMyCode": true
    }
  ]
}

Enabling logToFile and comparing the logs between my laptop and desktop doesn't show any difference, apart from the timeout happening, and the port used (port 33191 on my laptop, 35205 on my desktop, both using 127.0.0.1 as the host). Putting this port in my config doesn't work, but I'm also not completely sure where this port number comes from and how the debugger injects itself into the process.

It seems the debugger can find the process (I can select it from the dropdown) but somehow isn't able to actually connect to it. Does anyone know what might be wrong, or have any suggestions to point me in the right direction? I've tried a bunch of suggestions from other posts, but they don't see to work (mostly outdated or for running the current file instead of attaching to an existing process).

2
  • Is it effective to change the browser or restart? You can also try adding this line to the launch file: "timeout":20000 Commented Apr 12, 2022 at 8:03
  • 1
    @MingJie Thanks for the suggestions. Restarting didn't change anything, and 'timeout' doesn't seem to be a valid argument but attaching should happen pretty much instantly anyway. Commented Apr 13, 2022 at 9:11

1 Answer 1

4

Digging a bit deeper I found there are multiple log files, one of which had an error about ptrace_scope (which I'd never heard of).

Simply said the value of the file at /proc/sys/kernel/yama/ptrace_scope determines what kind of processes debuggers can access. The different values are:

  • 0: all processes can be debugged, as long as they have same uid.
  • 1: only a parent process can be debugged.
  • 2: Only admin can use ptrace
  • 3: No processes may be traced with ptrace.

(list from here)

This file was set to 0 on my laptop (where attaching worked) but on my desktop it was set to 1, so I updated this value to 0 and now debugging works at expected.

echo 0|sudo tee /proc/sys/kernel/yama/ptrace_scope

edit: I came across this issue on the VSCode repo:

https://github.com/microsoft/vscode/issues/146348

Sign up to request clarification or add additional context in comments.

1 Comment

I have the same issue but with a swift project, after reading the issue you linked in the answer, I may need to file a bug to Swift extension repo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.