Breakpoints don't work in threads created after `debugpy.listen()` is called
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
Research direction
Start by running the provided multithread_debugging.py reproduction with start_listener_at_index=5 and compare GUI breakpoints with breakpoint(). Trace the thread setup around debugpy.listen(); done means GUI breakpoints hit in threads started both before and after listen, matching the expected behavior.
Written by the indexing model from the issue text.
Description
Environment data
- debugpy version: 1.8.14 / 1.8.17
- OS and version: Mac OS 15.6.1
- Python version (& distribution if applicable, e.g. Anaconda): 3.10.12
- Using VS Code or Visual Studio: Visual Studio Code Version: 1.104.1
Actual behavior
In threads which are started (via threading.Thread.start()) after the call to debugpy.listen(), breakpoints will not work.
Expected behavior
Breakpoints should work in all threads, regardless of when they are started.
Steps to reproduce:
- Use the following script:
# multithread_debugging.py
import sys
import threading
from queue import Queue
import debugpy
threads: list[threading.Thread] = []
queues: list[Queue] = []
N_THREADS = 10
POISON_PILL = -1
def handler(q: Queue):
while True:
item = q.get()
print(threading.current_thread(), item)
if item == POISON_PILL:
print(threading.current_thread(), "exiting")
return
def send(i: int, item):
queues[i].put(item)
def end(i: int, *, wait=False):
send(i, POISON_PILL)
if wait:
threads[i].join()
def end_all():
for i in range(N_THREADS):
end(i)
for t in threads:
t.join()
print("all done")
if __name__ == "__main__":
start_listener_at_index = 0
if len(sys.argv) >= 2:
start_listener_at_index = int(sys.argv[1])
for i in range(10):
q = Queue()
t = threading.Thread(target=handler, args=[q])
threads.append(t)
queues.append(q)
for i, t in enumerate(threads):
if i == start_listener_at_index:
print(f"starting debug listener before thread {i} starts")
debugpy.listen(("0.0.0.0", 5680))
t.start()
- Add the following launch configuration:
{
"name": "Attach to multithreading example",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5680
}
},
- Run
python -i multithread_debugging.py 5 - Add a breakpoint on the print statement in the
handler()method. - Run the "Attach to multithreading example" debug configuration.
- In the REPL, run:
send(0, "foo"). Note that the breakpoint is not triggered. - In the REPL, run:
send(4, "foo"). Note that the breakpoint is not triggered. - In the REPL, run
send(5, "foo"). Note that the breakpoint IS triggered (because thread 5 is started after the debugpy listen call).
Further notes
If you set the breakpoint using the breakpoint() function, it works in all threads. However, if you set it using the vscode GUI, you get the buggy behaviour described above.
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 202
- Avg merge
- 5d 1h
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/debugpy
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
All issues in microsoft/debugpy
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
canonical/paas-charm#368 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
tech debt
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
StevenBlack/hosts#3256 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
qualcomm/qai-appbuilder#275 ·