From 06e2fbb8260022de8532d2e940fc69e8ea413679 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Wed, 22 Nov 2023 18:12:23 +0900 Subject: [PATCH] [Bug #19114] Fix for multiple calls of TracePoint#enable --- test/ruby/test_settracefunc.rb | 12 ++++++++++++ vm_trace.c | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index 8fcc909d83..b7711e191d 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -2459,6 +2459,18 @@ CODE assert_equal [:tp1, 1, 2, :tp2, 3], events end + def test_multiple_enable + ary = [] + trace = TracePoint.new(:call) do |tp| + ary << tp.method_id + end + trace.enable + trace.enable + foo + trace.disable + assert_equal(1, ary.count(:foo), '[Bug #19114]') + end + def test_multiple_tracepoints_same_bmethod events = [] tp1 = TracePoint.new(:return) do |tp| diff --git a/vm_trace.c b/vm_trace.c index 4b97288f23..6969c8d3ce 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -1197,6 +1197,10 @@ rb_tracepoint_enable(VALUE tpval) rb_raise(rb_eArgError, "can't nest-enable a targeting TracePoint"); } + if (tp->tracing) { + return Qundef; + } + if (tp->target_th) { rb_thread_add_event_hook2(tp->target_th->self, (rb_event_hook_func_t)tp_call_trace, tp->events, tpval, RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);