Project

General

Profile

Actions

Bug #20059

closed

TracePoint#enable can be called multiple times, increasing the number of invokations

Added by zverok (Victor Shepelev) 12 months ago. Updated 11 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:115701]

Description

I just stumbled upon this accidentally (created a tracepoint with TracePoint.trace, and then enabled 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?..

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0