Skip to content

Commit 9919790

Browse files
authored
ref(profiling): unref timer (#12340)
Tentative fix for #12169 I did not generate a core dump, but by looking at the crash, it seemed like it the segfault was happening after the bindings were required and the code after the require statement had already ran, which hints at an issue with the measurement collection loop. This is a small change, but it ensures that the reference to the timer is not maintained and can be properly collected, as well as adds a missing nullptr check.
1 parent b85f88e commit 9919790

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

packages/profiling-node/bindings/cpu_profiler.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class MeasurementsTicker {
7676
MeasurementsTicker(uv_loop_t *loop)
7777
: period_ms(100), isolate(v8::Isolate::GetCurrent()) {
7878
uv_timer_init(loop, &timer);
79-
timer.data = this;
79+
uv_handle_set_data(reinterpret_cast<uv_handle_t *>(&timer), this);
80+
uv_unref(reinterpret_cast<uv_handle_t *>(&timer));
8081
}
8182

8283
static void ticker(uv_timer_t *);
@@ -196,6 +197,10 @@ void MeasurementsTicker::cpu_callback() {
196197
};
197198

198199
void MeasurementsTicker::ticker(uv_timer_t *handle) {
200+
if (handle == nullptr) {
201+
return;
202+
}
203+
199204
MeasurementsTicker *self = static_cast<MeasurementsTicker *>(handle->data);
200205
self->heap_callback();
201206
self->cpu_callback();
@@ -323,18 +328,6 @@ void SentryProfile::Start(Profiler *profiler) {
323328
status = ProfileStatus::kStarted;
324329
}
325330

326-
static void CleanupSentryProfile(Profiler *profiler,
327-
SentryProfile *sentry_profile,
328-
const std::string &profile_id) {
329-
if (sentry_profile == nullptr) {
330-
return;
331-
}
332-
333-
sentry_profile->Stop(profiler);
334-
profiler->active_profiles.erase(profile_id);
335-
delete sentry_profile;
336-
};
337-
338331
v8::CpuProfile *SentryProfile::Stop(Profiler *profiler) {
339332
// Stop the CPU Profiler
340333
v8::CpuProfile *profile = profiler->cpu_profiler->StopProfiling(
@@ -376,6 +369,18 @@ const uint16_t &SentryProfile::cpu_usage_write_index() const {
376369
return cpu_write_index;
377370
};
378371

372+
static void CleanupSentryProfile(Profiler *profiler,
373+
SentryProfile *sentry_profile,
374+
const std::string &profile_id) {
375+
if (sentry_profile == nullptr) {
376+
return;
377+
}
378+
379+
sentry_profile->Stop(profiler);
380+
profiler->active_profiles.erase(profile_id);
381+
delete sentry_profile;
382+
};
383+
379384
#ifdef _WIN32
380385
static const char kPlatformSeparator = '\\';
381386
static const char kWinDiskPrefix = ':';
@@ -1049,6 +1054,7 @@ void FreeAddonData(napi_env env, void *data, void *hint) {
10491054

10501055
if (profiler->cpu_profiler != nullptr) {
10511056
profiler->cpu_profiler->Dispose();
1057+
profiler->cpu_profiler = nullptr;
10521058
}
10531059

10541060
delete profiler;

0 commit comments

Comments
 (0)