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


Eregon (Benoit Daloze) wrote in #note-5:
> But I think `trace.enable { ... }` should not affect other Threads/Fiber, otherwise that form is just too confusing and error-prone.

If you look at the tests, they expect that `enable` is not limited to the current thread.  The expected usage is tracing other threads during the duration of the block.  You can already specify to only trace the current thread using: `enable(target_thread: Thread.current)`.  I do think `enable` only affecting the current thread may be a more common desire, but I can also see that `enable` allowing tracing other threads inside the block is also a reasonable use.  I don't think it's worth breaking backwards compatibility to change the behavior.

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

* 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>