
Issue #20059 has been updated by tenderlovemaking (Aaron Patterson). 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) ---------------------------------------- Bug #20059: TracePoint#enable can be called multiple times, increasing the number of invokations https://bugs.ruby-lang.org/issues/20059#change-105633 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I just stumbled upon this accidentally (created a tracepoint with `TracePoint.trace`, and then `enable`d it). My initial example ```ruby 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"... ```ruby 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](https://docs.ruby-lang.org/en/master/TracePoint.html#method-i-enable) 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?.. -- https://bugs.ruby-lang.org/