Skip to content

Commit 9216eee

Browse files
authored
[Profiler] Force waiting for the LibrariesInfoCache service to be fully started (#6338)
1 parent b433853 commit 9216eee

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/AutoResetEvent.h

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ class AutoResetEvent
2121
~AutoResetEvent();
2222

2323
void Set();
24+
25+
// Wait will block the execution of the thread until a call to Set is made
26+
// or until it times out.
27+
// [parameter] timeout : time to wait before returning.
28+
// if timeout == InfiniteTimeout, it will wait until Set is called.
29+
// if timeout != InfiniteTimeout, it will wait either for a call to Set either for the timeout.
30+
// [Return]
31+
// true if Set was called, false is the call timed out.
2432
bool Wait(std::chrono::milliseconds timeout = InfiniteTimeout);
2533

2634
private:

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/LibrariesInfoCache.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ bool LibrariesInfoCache::StartImpl()
3333
_librariesInfo.reserve(100);
3434
s_instance = this;
3535
unw_set_iterate_phdr_function(unw_local_addr_space, LibrariesInfoCache::DlIteratePhdr);
36-
_worker = std::thread(&LibrariesInfoCache::Work, this);
37-
return true;
36+
auto startEvent = std::make_shared<AutoResetEvent>(false);
37+
_worker = std::thread(&LibrariesInfoCache::Work, this, startEvent);
38+
// We must wait for the thread to be fully started and the cache populated
39+
// before reporting the service start status.
40+
// 2s for CI
41+
return startEvent->Wait(2s);
3842
}
3943

4044
bool LibrariesInfoCache::StopImpl()
@@ -51,7 +55,7 @@ bool LibrariesInfoCache::StopImpl()
5155
return true;
5256
}
5357

54-
void LibrariesInfoCache::Work()
58+
void LibrariesInfoCache::Work(std::shared_ptr<AutoResetEvent> startEvent)
5559
{
5660
OpSysTools::SetNativeThreadName(WStr("DD_LibsCache"));
5761

@@ -80,6 +84,12 @@ void LibrariesInfoCache::Work()
8084
}
8185

8286
UpdateCache();
87+
88+
if (startEvent != nullptr)
89+
{
90+
startEvent->Set();
91+
startEvent.reset();
92+
}
8393
}
8494

8595
Log::Debug("Stopping worker: stop request received.");

profiler/src/ProfilerEngine/Datadog.Profiler.Native.Linux/LibrariesInfoCache.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <atomic>
1414
#include <libunwind.h>
1515
#include <link.h>
16+
#include <memory>
1617
#include <shared_mutex>
1718
#include <thread>
1819
#include <vector>
@@ -48,7 +49,7 @@ class LibrariesInfoCache : public ServiceBase
4849
#ifdef DD_TEST
4950
private:
5051
#endif
51-
void Work();
52+
void Work(std::shared_ptr<AutoResetEvent> startEvent);
5253

5354
static LibrariesInfoCache* s_instance;
5455

0 commit comments

Comments
 (0)