Project

General

Profile

Actions

Bug #20059

closed

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

Added by zverok (Victor Shepelev) 5 months ago. Updated 4 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?..

Updated by tenderlovemaking (Aaron Patterson) 5 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) 5 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?

Actions #3

Updated by mame (Yusuke Endoh) 4 months ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0