Issue #16889 has been updated by jeremyevans0 (Jeremy Evans).


mame (Yusuke Endoh) wrote in #note-7:
> `TracePoint#enable(target: :block)` does not follow events in a called method. Is this really intentional?
> 
> ```
> trace = TracePoint.new(:line) {|tp| p tp }
> 
> def foo
>   1
> end
> 
> trace.enable do
>   foo
> end
> #=>
> # #<TracePoint:line t.rb:8>
> # #<TracePoint:line t.rb:4 in `foo'>
> 
> puts
> 
> trace.enable(target: :block) do
>   foo
> end
> #=>
> # #<TracePoint:line t.rb:17>
> ```
> 
> Note that the second `trace.enable` does not print `#<TracePoint:line t.rb:4 in 'foo'>`.

This matches the behavior of passing the block explicitly and using it as the target:

```ruby
trace = TracePoint.new(:line) {|tp| p tp }

def foo
  1
end

trace.enable do
  foo
end
#=>
# #<TracePoint:line t.rb:8>
# #<TracePoint:line t.rb:4 in `foo'>

puts

pr = proc do
  foo
end

trace.enable(target: pr, &pr) 

#<TracePoint:line t.rb:17>
```

So I think it is expected.  My guess would be while the location of the trace point shows the point of call, it isn't triggered unless the method itself is targeted.  If you target both the method and the block, you get both lines:

```ruby
trace = TracePoint.new(:line) {|tp| p tp }
trace2 = TracePoint.new(:line) {|tp| p tp }

def foo
  1
end

trace.enable do
  foo
end
#=>
# #<TracePoint:line t.rb:8>
# #<TracePoint:line t.rb:4 in `foo'>

puts

pr = proc do
  foo
end

trace2.enable(target: method(:foo)) do
  trace.enable(target: pr, &pr)
end
# #<TracePoint:line -:18>
# #<TracePoint:line -:5 in `foo'>
```

Whether the current behavior of targeted traces is a bug, I'm not sure. If so, it isn't a bug in the `target: :block` support, but a more general issue with targeted traces.

----------------------------------------
Feature #16889: TracePoint.enable { ... } also activates the TracePoint for other threads, even outside the block
https://bugs.ruby-lang.org/issues/16889#change-93303

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
----------------------------------------
```ruby
threads = []
inspects = []
trace = TracePoint.new(:line) do |tp|
  threads << Thread.current
  inspects << tp.inspect
end

done = false
thread = Thread.new do
  Thread.pass until done
end

trace.enable do
  line_event = true
  done = true
  sleep 1
end
thread.join

# Expected only within enable block (lines 14-16)
puts inspects

# Expected just 1
p threads.uniq
```

Results in:
```
$ ruby tpbug.rb
ruby tpbug.rb
#<TracePoint:line / tpbug.rb:14>
#<TracePoint:line / tpbug.rb:15>
#<TracePoint:line / tpbug.rb:16>
#<TracePoint:line / tpbug.rb:10>
[#<Thread:0x00005571134e3340 run>, #<Thread:0x00005571138ac828 / tpbug.rb:9 dead>]
```

But I expected:
```
#<TracePoint:line / tpbug.rb:14>
#<TracePoint:line / tpbug.rb:15>
#<TracePoint:line / tpbug.rb:16>
[#<Thread:0x00005571134e3340 run>]
```

Because the RDoc says:
```
If a block is given, the trace will only be enabled within the scope of
the block.
```

For background I'm trying to improve the TracePoint specs in ruby/spec, but they are proving quite unreliable due to this.

@ko1 Thoughts?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>