jruby icon indicating copy to clipboard operation
jruby copied to clipboard

Tracing `:call` doesn't work if the traced code is running on a different thread

Open mauricioszabo opened this issue 9 months ago • 3 comments

Environment Information

  • JRuby version: jruby 10.0.0.0-SNAPSHOT (3.4.2) 2025-03-13 a4e4d7dc4c OpenJDK 64-Bit Server VM 21.0.6+7-Ubuntu-124.04.1 on 21.0.6+7-Ubuntu-124.04.1 +indy +jit [x86_64-linux]
  • Operating system and platform: Ubuntu 24.04 / 6.8.0-1010-nvidia #10-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 15 15:04:16 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Expected Behavior

TracePoint should work if we start a new thread. The following code shows the issue:

trace = TracePoint.new(:call) do |tp|
  puts "Tracing: #{tp.path}:#{tp.lineno}"
end
trace.enable

def something(a, b)
  result = yield a, b
  "Result: #{result}"
end

def make_sum(a, b)
  a + b
end

puts "Without a thread:"
something(10, 20) do |a, b|
  make_sum(a, b)
end

puts "\nWith a thread:"
something(10, 20) do |a, b|
  Thread.new do
    make_sum(a, b)
  end.join
end

On MRI, this prints:

Without a thread:
Tracing: /tmp/trace_example.rb:6
Tracing: /tmp/trace_example.rb:11

With a thread:
Tracing: /tmp/trace_example.rb:6
Tracing: /tmp/trace_example.rb:11

But on JRuby, this prints:

Without a thread:
Tracing: /tmp/trace_example.rb:6
Tracing: /tmp/trace_example.rb:11

With a thread:
Tracing: /tmp/trace_example.rb:6

Exporting JRUBY_OPTS='--debug' doesn't change the output

mauricioszabo avatar Mar 14 '25 01:03 mauricioszabo

If it's just a matter of inheriting the tracing status from the parent thread, this ought to be pretty easy.

headius avatar Mar 14 '25 03:03 headius

This actually works fine in 9.4.x, and regressed in 10 (which probably added logic to localize events to specific threads).

$ jruby -v --debug blah.rb    
jruby 9.4.14.0-SNAPSHOT (3.1.7) 2025-08-20 a2ce292820 OpenJDK 64-Bit Server VM 25.362-b09 on 1.8.0_362-b09 +jit [arm64-darwin]
Without a thread:
Tracing: blah.rb:6
Tracing: blah.rb:11

With a thread:
Tracing: blah.rb:6
Tracing: blah.rb:11

headius avatar Aug 20 '25 20:08 headius

Regressed before 10.0.0.0 release via #8163 by @enebo. I guess limiting it to a single thread forever is not quite right.

headius avatar Aug 20 '25 20:08 headius