vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Race condition between STOPPED and THREAD events in the debug service

Open edumunoz opened this issue 10 years ago • 10 comments

  • VSCode Version: 0.10.12-insider / All
  • OS Version: Ubuntu 14.04
  • Debugging using Microsoft cpp debugger, but will happen in any "stop-all" style debugger. Note that this is an edge case, when a STOPPED event is sent by the debug adapter very close to a new thread creation. Also, this is not an immediate necessity. An April release fix is good.

Steps to Reproduce:

  1. Compile the following C++ code https://gist.github.com/edumunoz/80275bd231abb56165eb g++ -g -std=c++11 -pthread vscode-repro.cpp -o vscode-repro
  2. Set a breakpoint in line 7 (the body of the test() function)
  3. Start debugging. After hitting continue a couple of times, some of the threads won't have a callstack even if expanded.

In order to see it repro more clear, you can see the logging I added here https://github.com/edumunoz/vscode/commit/01a0e84d1b7bf613a027fd50764884de5e4c3192

[2438:0323/114759:INFO:CONSOLE(198)] "new thread has started 8", source: file:////home/edmunoz/code/vscode/out/vs/workbench/parts/debug/electron-browser/debugService.js (198)
[2438:0323/114759:INFO:CONSOLE(169)] "Recevied stopped event. The considered threads are  1 5 7", source: file:////home/edmunoz/code/vscode/out/vs/workbench/parts/debug/electron-browser/debugService.js (169)
[2438:0323/114759:INFO:CONSOLE(200)] "now received the info about the new thread 8", source: file:////home/edmunoz/code/vscode/out/vs/workbench/parts/debug/electron-browser/debugService.js (200)

The debug adapter is sending the THREAD event before the STOPPED event (which is correct). However, since the DebugService queries the debug adapter for all the threads after getting a started thread event, there is a race condition between STOPPED and THREAD.

The result of this race condition is that, since by default threads are created with stopped = false, VSCode doesn't query for the callstack of those threads when calling getChildren on the CallStackDataSource.

/cc @isidorn @weinand @delmyers

edumunoz avatar Mar 23 '16 19:03 edumunoz

@edumunoz Thanks for the nice issue report. Though it might have been less work if you submited a PR instead. Do you have a suggestion for a fix?

isidorn avatar Apr 14 '16 15:04 isidorn

I'm not sure whether other race conditions exist. I would expect the handler for the STOPPED event to wait if there are any pending thread updates.

I guess the STOPPED event could query for all the threads and update the model.


Why is the THREAD event required, and not just have VSCode query for all the threads and update its model on a STOPPED event? I don't see the threads being used other than when the VSCode is in break mode.

edumunoz avatar Apr 15 '16 21:04 edumunoz

@edumunoz Thread events are optional. They only exist for debug adapters that want to report thread activity at times when threads are not stopped. If thread activity needs only to be updated on stopped events, there is no need for the debug adapter to send thread events.

weinand avatar Apr 18 '16 13:04 weinand

They only exist for debug adapters that want to report thread activity at times when threads are not stopped

@weinand It doesn't seem that way. The DebugService only updates the model with new threads when receiving a ThreadEvent (via a call to rawUpdate).

If a started ThreadEvent wasn't received for a specific thread, a StoppedEvent for that thread will cause that thread not to show in the CallStack viewlet, since it won't be in stored in the debug model.

Maybe I'm completely wrong and I missed a part where the threads are queried. How does VSCode use the threading information regarding threads that are not stopped?

edumunoz avatar Apr 18 '16 22:04 edumunoz

@isidorn please make sure that the DebugService works the way it is documented in http://code.visualstudio.com/docs/extensionAPI/api-debugging: "Whenever VS Code receives a stopped or a thread event, VS Code requests all threads that exist at that point in time and displays them if there are more than one. If only one thread is detected, the VS Code UI stays in single thread mode. Thread events are optional but a debug adapter can send them to force VS Code to update the threads UI dynamically even when not in a stopped state."

weinand avatar Apr 18 '16 22:04 weinand

The DebugService only updates the model with new threads when receiving a ThreadEvent (via a call to rawUpdate).

@edumunoz This does not seem to be true, the debugService updates the model both when receiving a ThreadEvent and a StoppedEvent.

So in your case you should not send a 'threads' event to VSCode, a 'stopped' event should be enough. I tried your reproducable case and changed vscode to ignore your thread event, in that case I still can not expand all threads - but I see they are stopped and the C++ adapter returns an empty call stack for them.

To print all the communication coming from the adapter unocmment this code please.

@weinand currently we are behaving like described in the api doc. However I am not sure we should send a 'thread' request to the adapter on a 'stopped' event. The 'stopped' event has all the details VSCode needs. Is there something I am missing here, why do we need to send a 'thread' request to the adapter in this case.

Closing this issue for now. Will reopen if @edumunoz still sees the issue, or if vscode does not behave as our documentation specifies.

isidorn avatar Apr 20 '16 16:04 isidorn

@isidorn we need to send a 'thread' request to the adapter because the 'stopped' event does not provide the list of threads. It just has one 'threadId' (which does not even include the thread's name). In order to render the list of threads you need all of them and their names. And this information is not part of the stopped event

weinand avatar Apr 20 '16 17:04 weinand

@weinand got it, there was an issue around this which should get fixed via b06cd81 so @edumunoz make sure to try it with that commit included

isidorn avatar Apr 20 '16 19:04 isidorn

Verified this is fixed for cpptools in master

andrewcrawley avatar Apr 26 '16 02:04 andrewcrawley

Adding the verified tag then :)

isidorn avatar Apr 26 '16 07:04 isidorn