fix: add ThreadLocal reentrancy guard to SpyImpl to prevent recursive advice dispatch#3161
Open
daguimu wants to merge 2 commits intoalibaba:masterfrom
Open
fix: add ThreadLocal reentrancy guard to SpyImpl to prevent recursive advice dispatch#3161daguimu wants to merge 2 commits intoalibaba:masterfrom
daguimu wants to merge 2 commits intoalibaba:masterfrom
Conversation
bfe1ef7 to
ff7e2b8
Compare
… advice dispatch When watching a toString() method, Arthas's own ObjectView.renderObject() calls toString() on the watched object to display results. This triggers the advice listener again on the same thread, causing infinite recursion. Fix by adding a ThreadLocal<Boolean> DISPATCHING guard to SpyImpl. When already dispatching advice on the current thread, re-entrant calls from the same thread are skipped, preventing recursion. Applies guard to atEnter(), atExit(), and atExceptionExit(). Zero overhead on non-recursive paths (single ThreadLocal read). Fixes alibaba#3083
ff7e2b8 to
b552f81
Compare
…thod coverage - Add debug logging when reentrancy is detected for easier troubleshooting - Extend DISPATCHING guard to trace invoke methods (atBeforeInvoke, atAfterInvoke, atInvokeException) which have the same recursive dispatch risk Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
watchto intercept atoString()method, Arthas's own output rendering code (ObjectView.renderObject()) callstoString()on the watched object to display results. This call re-triggers the advice listener on the same thread, causing infinite recursion.Steps to reproduce:
If
MyClass.toString()is called by Arthas's rendering pipeline, it re-fires the watch, which renders again, which callstoString()again — infinite loop.Root Cause
SpyImpl.atEnter/atExit/atExceptionExithad no guard against re-entrant calls on the same thread.Fix
Add a
ThreadLocal<Boolean> DISPATCHINGguard toSpyImpl. Before dispatching advice listeners, check if we are already dispatching on the current thread. If so, return early to break the re-entrancy cycle.The guard is reset in a
finallyblock to ensure correctness even if an exception occurs during dispatch.Impact
atEnter,atExit,atExceptionExitare guarded; trace invoke methods are unaffectedFixes #3083