Skip to content

Guard re-entry in rbs test hooks - #3383

Merged
ksss merged 1 commit into
ruby:masterfrom
ksss:fix-hook-reentry
Dec 3, 2026
Merged

ksss merged 1 commit into
ruby:masterfrom
ksss:fix-hook-reentry

Conversation

@ksss

@ksss ksss commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Fixes problem 5 of #3369: rbs test dies with SystemStackError when the target is a core class reopened by the program, like Integer or Hash.

class Integer
  def double = self * 2
end
p 21.double
$ RBS_TEST_TARGET=Integer RBS_TEST_OPT="-I sig" ruby -r rbs/test/setup example.rb
.../logger.rb:384:in 'Logger#level': stack level too deep (SystemStackError)

The generated hook calls methods that can be hooked themselves (Hash#[] for ORIGINAL_METHODS, Integer#< through Logger, Class#new for ArgumentsReturn/CallTrace, the type checker, ...), and nothing guards re-entry.

Changes

  • Add a fiber-local re-entry guard (RBS::Test::Hook.guard). A hooked method called inside the guard calls the original method directly, without type checking.
  • The hooks run logging, bookkeeping, and Observer.notify (type checking) inside the guard.
  • The call to the original method and the user's block stay outside the guard, so hooked methods called from the method under test are still type checked.
  • The guard and the pass-through path call their primitives (Thread.current, Thread#[], Thread#[]=, Hash#[], __send__, ...) through UnboundMethods captured at load time, which keep pointing to the original definitions after hooking. So the only method that cannot be hooked is UnboundMethod#bind_call; Tester#install! skips it with a log message.
  • setup.rb runs its TracePoint handler (including install!) inside the guard, so rbs test's own code isn't type checked.

Not in scope

RBS_TEST_TARGET=Kernel with a script reopening module Kernel no longer recurses, but still fails because frame-sensitive methods like __method__ get hooked (problem 9 of #3369).

🤖 Generated with Claude Code

Hooking a core class reopened by the program, like `Integer` or `Hash`,
caused infinite recursion, because the hooks call methods that can be
hooked themselves (`Hash#[]`, `Integer#<` through `Logger`, ...).

The hooks now run their bookkeeping and type checking inside a guard,
and hooked methods called inside it call the original method directly.
The original method is called outside of the guard, so hooked methods
called from it are still type checked. The guard calls its primitives
through `bind_call`, and `UnboundMethod#bind_call` itself is never hooked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ksss
ksss added this pull request to the merge queue Dec 3, 2026
Merged via the queue into ruby:master with commit 6d43ffb Dec 3, 2026
23 checks passed
@ksss
ksss deleted the fix-hook-reentry branch December 3, 2026 08:00
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