Skip to content
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

ref(profiling) use cleanup instead of destructor #13661

Merged
merged 3 commits into from
Sep 16, 2024
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
31 changes: 16 additions & 15 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 @@ -87,9 +87,10 @@ 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));
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 *);
Expand All @@ -112,13 +113,13 @@ class MeasurementsTicker {
size_t listener_count();

~MeasurementsTicker() {
uv_timer_stop(&timer);
uv_handle_t *handle = (uv_handle_t *)timer;

auto handle = reinterpret_cast<uv_handle_t *>(&timer);
uv_timer_stop(timer);
uv_unref(handle);

// Calling uv_close on an inactive handle will cause a segfault.
if (uv_is_active(handle)) {
uv_close(handle, nullptr);
if (!uv_is_closing(handle)) {
uv_close(handle, [](uv_handle_t *handle) { delete handle; });
}
}
};
Expand All @@ -143,8 +144,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 @@ -154,7 +155,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 @@ -223,8 +224,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 @@ -233,7 +234,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
Loading