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