Skip to content

Commit

Permalink
Fix potential crash when GC event was observed from thread without st…
Browse files Browse the repository at this point in the history
…ate doing GC

The state initialization code is somewhat subtle...
  • Loading branch information
ivoanjo committed Mar 29, 2024
1 parent ec1f4c3 commit 0c8da9e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ext/gvl_tracing_native_extension/gvl_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ static void on_thread_event(rb_event_flag_t event_id, const rb_internal_thread_e
thread_local_state *state = GT_EVENT_LOCAL_STATE(event_data,
// These events are guaranteed to hold the GVL, so they can allocate
event_id & (RUBY_INTERNAL_THREAD_EVENT_STARTED | RUBY_INTERNAL_THREAD_EVENT_RESUMED));

if (!state) return;

#ifdef RUBY_3_3_PLUS
if (!state->thread) state->thread = event_data->thread;
#endif
Expand Down Expand Up @@ -299,6 +301,9 @@ static void on_thread_event(rb_event_flag_t event_id, const rb_internal_thread_e
static void on_gc_event(VALUE tpval, UNUSED_ARG void *_unused1) {
const char* event_name = "bug_unknown_event";
thread_local_state *state = GT_LOCAL_STATE(rb_thread_current(), false); // no alloc during GC

if (!state) return;

switch (rb_tracearg_event_flag(rb_tracearg_from_tracepoint(tpval))) {
case RUBY_INTERNAL_EVENT_GC_ENTER: event_name = "gc"; break;
// TODO: is it possible the thread wasn't running? Might need to save the last state.
Expand Down

0 comments on commit 0c8da9e

Please sign in to comment.