Skip to content

Keep TracePoint event data per thread instead of locking the hook - #10102

Open
sampokuokkanen wants to merge 1 commit into
jruby:masterfrom
sampokuokkanen:tracepoint-thread-state
Open

sampokuokkanen wants to merge 1 commit into
jruby:masterfrom
sampokuokkanen:tracepoint-thread-state

Conversation

@sampokuokkanen

@sampokuokkanen sampokuokkanen commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

A TracePoint's hook held a lock while its block ran, because the event data was stored on the TracePoint object. If a hook waits for another thread and that thread hits the same TracePoint, both threads hang. The debug gem does this: it stops inside a breakpoint's hook and waits for its session thread, which runs code the same TracePoint traces. So the debugger hangs as soon as a breakpoint is hit (ruby/debug#1120).

def other_m = :other
def main_m = :main
main = Thread.current
go = Queue.new
seen = {}
thread = Thread.new { go.pop; other_m }
tp = TracePoint.new(:call) do |t|
  next unless t.path == __FILE__
  if Thread.current == main
    go << true
    seen[:joined] = !thread.join(5).nil?
  else
    seen[:other] = t.method_id
  end
end
tp.enable
begin
  main_m
ensure
  tp.disable
end
thread.join
p seen
$ jruby --debug tp_lock.rb   # before
{joined: false, other: :other_m}
$ jruby --debug tp_lock.rb   # after, and CRuby
{other: :other_m, joined: true}

The event data now lives on the thread while the hook runs, as CRuby keeps it per execution context, and the hook takes no lock. As in CRuby, a TracePoint's attributes read the event of the hook running on the current thread, and TracePoint#self raises "access from outside" outside a hook.

The hook of one TracePoint can now run on several threads at once, as set_trace_func hooks already could, and TracePoint#disable returns without waiting for hooks still running on other threads. A hook block that changes shared state must synchronize itself.

A TracePoint's hook held a lock while its block ran, because the event
data was stored on the TracePoint object. When a hook stopped and
waited for another thread, and that thread hit the same TracePoint,
both threads hung. The debug gem does this: it stops inside a
breakpoint's hook and waits for its session thread, which runs Ruby
code that the same TracePoint traces.

The event data is now kept on the thread while the hook runs, and the
hook no longer takes a lock. This follows CRuby, which keeps the trace
argument per execution context. As in CRuby, a TracePoint's attributes
read the event of the hook running on the current thread, and
TracePoint#self now raises "access from outside" outside a hook like
the other attributes.

The hook of one TracePoint can now run on several threads at once, as
set_trace_func hooks already could, and TracePoint#disable returns
without waiting for hooks still running on other threads. A hook block
that changes shared state, like a Hash of counts, must synchronize on
its own.
@sampokuokkanen
sampokuokkanen force-pushed the tracepoint-thread-state branch from 3e93be7 to 198f5c4 Compare November 18, 2026 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant