Bug #20059
closedTracePoint#enable can be called multiple times, increasing the number of invokations
Description
I just stumbled upon this accidentally (created a tracepoint with TracePoint.trace
, and then enable
d it).
My initial example
trace = TracePoint.trace(:raise) do |tp|
puts "Exception raised: #{tp.raised_exception.inspect} at #{tp.path}:#{tp.lineno}"
end
trace.enable # shouldn't do this, it was already enabled!
raise "foo"
This prints
Exception raised: #<RuntimeError: foo> at test.rb:5
Exception raised: #<RuntimeError: foo> at test.rb:5
Twice.
If I'll remove the "unnecessary" trace.enable
, it prints it once.
So the theory is "multiple enables = multiple invokations of a tracepoint"...
trace = TracePoint.trace(:raise) do |tp|
puts "Exception raised: #{tp.raised_exception.inspect} at #{tp.path}:#{tp.lineno}"
end
5.times { trace.enable }
raise "foo"
...and indeed, this code prints the "Exception raised:" message 6 times.
I don't see anything about this behavior in the method's docs or class docs, and can't think of a plausible explanation. But it was this way since Ruby 2.0, and either it should be this way, or it bothers nobody :)
I wonder:
- is there a reasonable explanation for this?
- if so, shouldn't it be documented somewhere?..
Or is it some part of a pattern of how the TracePoint
works that I am missing here?..
Updated by tenderlovemaking (Aaron Patterson) 12 months ago
IIRC the tracepoints are just added to a linked list. There's probably no validation as to whether or not that tracepoint has already been added to the linked list (and we just need to do that book keeping)
Updated by kyanagi (Kouhei Yanagita) 12 months ago
Is this issue the same as #19114?
I have created a pull request for the fix at https://github.com/ruby/ruby/pull/8993.
Does this resolve the problem?