This is an archive of the discontinued LLVM Phabricator instance.

Fix race condition when launching and attaching.
ClosedPublic

Authored by clayborg on Feb 14 2022, 5:12 PM.

Details

Summary

We discovered that when using "launchCommands" or "attachCommands" that there was an issue where these commands were not being run synchronously. There were further problems in this case where we would get thread events for the process that was just launched or attached before the IDE was ready, which is after "configurationDone" was sent to lldb-vscode.

This fix introduces the ability to wait for the process to stop after the run or attach to ensure that we have a stopped process at the entry point that is ready for the debug session to proceed. This also allows us to run the normal launch or attach without needing to play with the async flag the debugger. We spin up the thread that listens for process events before we start the launch or attach, but we stop events from being delivered through the DAP protocol until the "configurationDone" packet is received. This function was manually delivering the first process stopped event in the code already, so this means we don't end up sending two stopped events at the start of a debug session.

Added a flag to the vscode.py protocol classes that detects and ensures that no "stopped" events are sent prior to "configurationDone" has been sent and will raise an error if it does happen.

This should make our launching and attaching more reliable and avoid some deadlocks that were being seen (https://reviews.llvm.org/D119548).

Diff Detail

Event Timeline

clayborg requested review of this revision.Feb 14 2022, 5:12 PM
clayborg created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2022, 5:12 PM
ilya-nozhkin added inline comments.Feb 14 2022, 6:04 PM
lldb/tools/lldb-vscode/VSCode.cpp
9

Incompatible with MSVC. Maybe use std::chrono instead?

Instead of polling, would it be possible for the event handler thread to send a notification (through a condition variable, std::future, etc.), and have the main thread wait for that?

But in general, avoiding flipping the modes back and forth seems like a good idea.

lldb/tools/lldb-vscode/lldb-vscode.cpp
799

is something missing here?

yinghuitan added inline comments.
lldb/tools/lldb-vscode/VSCode.cpp
544

Is this a typo? It should be 1000 instead of 10000, right?

I agree with labath. Specially because I wouldn't like the user having aborted debug sessions if their setup takes longer than 10 seconds to get the process ready, which I imagine might happen with android or oculus devices using custom launch configs. What about using a condition variable and at the same time issue a progress notification mentioning that the LLDB is waiting for the process to stop? They could stop LLDB manually if something is not working properly for them in that case.

clayborg updated this revision to Diff 409700.Feb 17 2022, 9:55 AM
  • use std::chrono for sleeping the function during polling
  • we must use polling in VSCode::WaitForProcessToStop since attach doesn't deliver the initial eStateStopped event
  • ignore any eStateStopped events that have a process stop ID of 1. This allows us to easily ignore the first stop that is delivered for launching, and if attaching ever does deliver this first event, it will be ignored. We do this because the IDE expects no process stopped events before "configurationDone" is completed and also because request_configurationDone() will deliver the first stop event manually _if_ we are stopping at the entry ("stopOnEntry" from launch config), else it will auto resume the process for us
  • add a new "timeout" launch configuration argument for both launch and attach which defaults to 30 seconds if not specified. This controls how long we poll for the process to stop during launch or attach.
clayborg marked 2 inline comments as done.Feb 17 2022, 10:17 AM

Instead of polling, would it be possible for the event handler thread to send a notification (through a condition variable, std::future, etc.), and have the main thread wait for that?

I tried doing this, but was unable to make it work because attaching doesn't send the a eStateStopped event for the process' stop ID of 1 where launching does. Probably a bug that can be fixed in LLDB at some point, but attaching can be done in so many ways that I didn't want to hold up this patch for this.

I agree with labath. Specially because I wouldn't like the user having aborted debug sessions if their setup takes longer than 10 seconds to get the process ready, which I imagine might happen with android or oculus devices using custom launch configs. What about using a condition variable and at the same time issue a progress notification mentioning that the LLDB is waiting for the process to stop? They could stop LLDB manually if something is not working properly for them in that case.

I default to 30 seconds now and I have added a new "timeout" launch configuration argument that _can_ be specified and the timeout can be made longer. Let me know if the 30 second default timeout should be increased.

lldb/tools/lldb-vscode/VSCode.cpp
9

Done in the current updated patch, thanks for pointing this out.

544

Fixed by using std::chrono

wallace accepted this revision.Feb 17 2022, 11:36 AM

lgtm

lldb/tools/lldb-vscode/VSCode.cpp
533
This revision is now accepted and ready to land.Feb 17 2022, 11:36 AM
This revision was automatically updated to reflect the committed changes.
clayborg marked 2 inline comments as done.

Hi @clayborg this rev has caused some lldb-vscode test failures across all LLDB/Linux buildbots.

Hi @clayborg this rev has caused some lldb-vscode test failures across all LLDB/Linux buildbots.

tracking this down now

Fixed buildbots with:

commit 38054556a08884aa15d3ebc720e2f43d0cb5a944 (HEAD -> main, origin/main, origin/HEAD)
Author: Greg Clayton <gclayton@fb.com>
Date:   Thu Feb 17 23:59:15 2022 -0800

    Fix buildbots after https://reviews.llvm.org/D119797
    
    This value error is no longer needed with the new version of the patch

TestVSCode_attach.py is still failing. Are you able to fix that quickly or shall we revert?

There's also a timeout in TestVSCode_coreFile.py

Here is the diff that should fix things:

https://reviews.llvm.org/rGc41c57468949

Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2022, 11:48 AM

It worked. Thanks.