Issue #13483 has been updated by ko1 (Koichi Sasada).


After consideration, I found several problems.

There is a implicit expectation that we can emulate block accept call with begin/ensure like:

```
open do
  ...
end
#=> same as:
begin
  open
  yield
ensure
  close
end
```

However, this proposal breaks this expectation. So I reject this ticket.

I try to consider to introduce how to filter the probes, like:

```
trace = TracePoint.new(:line, thread: Thread.current){ ... }
trace.enable #=> only enable on the current thread.

trace = TracePoint.new(:line, file: __FILE__){ ... }
trace.enable #=> only enable on this file.

trace = TracePoint.new(:line, method: method(:foo)){ ... }
trace.enable #=> only enable on `foo` method.
```

Thanks,
Koichi


----------------------------------------
Feature #13483: TracePoint#enable with block for thread-local trace
https://bugs.ruby-lang.org/issues/13483#change-65100

* Author: ko1 (Koichi Sasada)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* Target version: 2.5
----------------------------------------
# Summary

`TracePoint#enable` with block should enable thread-local trace.

# Current behavior

`TracePoint#enable` enables TracePoint for all of threads, even if it called with `do...end` blcok.

```ruby
t1 = Thread.new{
  loop{
    x = 1
  }
}
th = nil
trace = TracePoint.new(:line){|tp|
  if th != Thread.current
    p th = Thread.current
  end
}

trace.enable do
  loop{
    a = 1
    b = 2
  }
end
```

This program shows both main thread and thread `t1` hooked by line events.

# Problem

However, usually `trace.enable do ... end` imply the programmer want to enable hooks only for this block, not for other threads.
For example, Ruby's test for TracePoint skips hooks on other threads.
https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L620

# Proposal

`TracePoint#enable` with block should enable thread-local trace.
I believe proposed behavior is easy to use.

# Consideration

(1) It breaks backward compatibility. Is it acceptable?
(2) What happen on created threads? Should inherit thread-local hooks or ignore them?

I want to ask users of `TracePoint`.

Thanks,
Koichi



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