Skip to content

Commit

Permalink
fix(profiling) segfault on close
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Sep 10, 2024
1 parent 0812b24 commit b9a3c02
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions packages/profiling-node/bindings/cpu_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ enum class ProfileStatus {

class MeasurementsTicker {
private:
uv_timer_t timer;
uv_timer_t* timer;
uint64_t period_ms;
std::unordered_map<std::string,
const std::function<bool(uint64_t, v8::HeapStatistics &)>>
Expand All @@ -86,13 +86,15 @@ class MeasurementsTicker {

public:
MeasurementsTicker(uv_loop_t *loop)
: period_ms(100), isolate(v8::Isolate::GetCurrent()) {
uv_timer_init(loop, &timer);
uv_handle_set_data(reinterpret_cast<uv_handle_t *>(&timer), this);
uv_unref(reinterpret_cast<uv_handle_t *>(&timer));
: period_ms(100),
isolate(v8::Isolate::GetCurrent()) {
timer = new uv_timer_t;
uv_timer_init(loop, timer);
uv_handle_set_data((uv_handle_t*)timer, this);
uv_ref((uv_handle_t*)timer);
}

static void ticker(uv_timer_t *);
static void ticker(uv_timer_t*);
// Memory listeners
void heap_callback();
void add_heap_listener(
Expand All @@ -111,9 +113,17 @@ class MeasurementsTicker {

size_t listener_count();

void Cleanup() {
uv_timer_stop(&timer);
uv_close(reinterpret_cast<uv_handle_t *>(&timer), nullptr);
~MeasurementsTicker() {
uv_handle_t* handle = (uv_handle_t*)timer;

uv_timer_stop(timer);
uv_unref(handle);

if (!uv_is_closing(handle)) {
uv_close(handle, [](uv_handle_t *handle) {
delete handle;
});
}
}
};

Expand All @@ -137,8 +147,8 @@ void MeasurementsTicker::add_heap_listener(
heap_listeners.emplace(profile_id, cb);

if (listener_count() == 1) {
uv_timer_set_repeat(&timer, period_ms);
uv_timer_start(&timer, ticker, 0, period_ms);
uv_timer_set_repeat(timer, period_ms);
uv_timer_start(timer, ticker, 0, period_ms);
}
}

Expand All @@ -148,7 +158,7 @@ void MeasurementsTicker::remove_heap_listener(
heap_listeners.erase(profile_id);

if (listener_count() == 0) {
uv_timer_stop(&timer);
uv_timer_stop(timer);
}
};

Expand Down Expand Up @@ -202,12 +212,12 @@ void MeasurementsTicker::cpu_callback() {
uv_free_cpu_info(cpu, count);
};

void MeasurementsTicker::ticker(uv_timer_t *handle) {
void MeasurementsTicker::ticker(uv_timer_t* handle) {
if (handle == nullptr) {
return;
}

MeasurementsTicker *self = static_cast<MeasurementsTicker *>(handle->data);
MeasurementsTicker *self = static_cast<MeasurementsTicker*>(handle->data);
self->heap_callback();
self->cpu_callback();
}
Expand All @@ -217,8 +227,8 @@ void MeasurementsTicker::add_cpu_listener(
cpu_listeners.emplace(profile_id, cb);

if (listener_count() == 1) {
uv_timer_set_repeat(&timer, period_ms);
uv_timer_start(&timer, ticker, 0, period_ms);
uv_timer_set_repeat(timer, period_ms);
uv_timer_start(timer, ticker, 0, period_ms);
}
}

Expand All @@ -227,7 +237,7 @@ void MeasurementsTicker::remove_cpu_listener(
cpu_listeners.erase(profile_id);

if (listener_count() == 0) {
uv_timer_stop(&timer);
uv_timer_stop(timer);
}
};

Expand Down Expand Up @@ -1149,7 +1159,6 @@ void FreeAddonData(napi_env env, void *data, void *hint) {
profiler->cpu_profiler = nullptr;
}

profiler->measurements_ticker.Cleanup();
delete profiler;
}

Expand Down

0 comments on commit b9a3c02

Please sign in to comment.