Home > database >  VS Code Python debugger won't connect to running process (timeout)
VS Code Python debugger won't connect to running process (timeout)

Time:04-14

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).

CodePudding user response:

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
  • Related