Skip to content

Commit

Permalink
raise runtime error if coverage is stopped by a different thread than…
Browse files Browse the repository at this point in the history
… one that started it
  • Loading branch information
anmarchenko committed Jun 10, 2024
1 parent 573eeda commit 020b5fc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ext/datadog_cov/datadog_cov.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ struct dd_cov_data

uintptr_t last_filename_ptr;

// for single threaded mode: thread that is being covered
VALUE th_covered;

int threading_mode;
};

static void dd_cov_mark(void *ptr)
{
struct dd_cov_data *dd_cov_data = ptr;
rb_gc_mark_movable(dd_cov_data->coverage);
rb_gc_mark_movable(dd_cov_data->th_covered);
}

static void dd_cov_free(void *ptr)
Expand All @@ -52,6 +56,7 @@ static void dd_cov_compact(void *ptr)
{
struct dd_cov_data *dd_cov_data = ptr;
dd_cov_data->coverage = rb_gc_location(dd_cov_data->coverage);
dd_cov_data->th_covered = rb_gc_location(dd_cov_data->th_covered);
}

const rb_data_type_t dd_cov_data_type = {
Expand Down Expand Up @@ -188,6 +193,7 @@ static VALUE dd_cov_start(VALUE self)
{
VALUE thval = rb_thread_current();
rb_thread_add_event_hook(thval, dd_cov_update_coverage, RUBY_EVENT_LINE, self);
dd_cov_data->th_covered = thval;
}
else
{
Expand All @@ -205,7 +211,13 @@ static VALUE dd_cov_stop(VALUE self)
if (dd_cov_data->threading_mode == SINGLE_THREADED_COVERAGE_MODE)
{
VALUE thval = rb_thread_current();
rb_thread_remove_event_hook(thval, dd_cov_update_coverage);
if (!rb_equal(thval, dd_cov_data->th_covered))
{
rb_raise(rb_eRuntimeError, "Coverage was not started by this thread");
}

rb_thread_remove_event_hook(dd_cov_data->th_covered, dd_cov_update_coverage);
dd_cov_data->th_covered = Qnil;
}
else
{
Expand Down

0 comments on commit 020b5fc

Please sign in to comment.