From: "jeremyevans0 (Jeremy Evans)" Date: 2021-08-17T05:24:59+00:00 Subject: [ruby-core:104939] [Ruby master Feature#16889] TracePoint.enable { ... } also activates the TracePoint for other threads, even outside the block 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 > #=> > # # > # # > > puts > > trace.enable(target: :block) do > foo > end > #=> > # # > ``` > > Note that the second `trace.enable` does not print `#`. 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 #=> # # # # puts pr = proc do foo end trace.enable(target: pr, &pr) # ``` 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 #=> # # # # puts pr = proc do foo end trace2.enable(target: method(:foo)) do trace.enable(target: pr, &pr) end # # # # ``` 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 # # # # [#, #] ``` But I expected: ``` # # # [#] ``` 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: