Skip to content

[lldb] Print a message when background tasks take a while to complete… #8617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lldb/tools/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <bitset>
#include <clocale>
#include <csignal>
#include <future>
#include <string>
#include <thread>
#include <utility>
Expand Down Expand Up @@ -821,6 +822,18 @@ int main(int argc, char const *argv[]) {
}
}

SBDebugger::Terminate();
// When terminating the debugger we have to wait on all the background tasks
// to complete, which can take a while. Print a message when this takes longer
// than 1 second.
{
std::future<void> future =
std::async(std::launch::async, []() { SBDebugger::Terminate(); });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any risk on calling Terminate() on a different thread (as opposed to printing the message on another thread and terminating in the main thread?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't immediately think of a reason it wouldn't be safe to do, but I didn't look at every single terminator. Doing what you suggest is possible, though it would be slightly more convoluted and slightly less efficient. You'd have to spawn an async task and check periodically whether terminate has completed on the main thread. Something like:

bool terminated = false;
SBDebugger::Terminate();
terminated = true;
std::async(std::launch::async, [&terminated]() {
  for (size_t i = 0; i < 10; ++i) {
    if (terminated)
      return;
    std::this_thread::sleep_for(100ms);
   }
   fprintf(stderr, "Waiting for background tasks to complete...\n");
}

Let me know if you think that's worth the trade-off.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think it's safe, it's fine with me.


if (future.wait_for(std::chrono::seconds(1)) == std::future_status::timeout)
fprintf(stderr, "Waiting for background tasks to complete...\n");

future.wait();
}

return exit_code;
}