Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AOP增强两个嵌套调用的方法,子方法 afterReturning 主动执行ProcessController.throwsImmediately(new RuntimeException()); 存在的问题 #253

Closed
z529192557 opened this issue Jan 2, 2020 · 4 comments
Assignees
Labels

Comments

@z529192557
Copy link
Collaborator

z529192557 commented Jan 2, 2020

问题一: 子方法抛出异常无法被上级调用者afterThrowing感知
问题二:子方法抛出的异常, 被子方法自身的的afterThrowing 感知
重现步骤:
image
对 final int checkState(int a) 方法在afterReturning的时候抛出异常,并关注该方法afterThrowing事件
同时也关注final void checkState()的afterThrowing事件

期望结果:final void checkState() 可以响应 afterThrowing 事件
实际结果:final int checkState(int a) 方法响应afterThrowing事件了,final void checkState()未响应afterThrowing事件

分析:
1.根据字节码增强规则,Spy.onReturn 相关代码处在Sandbox的try-catch 逻辑中,因此onReturn主动抛出的异常会被try-catch 捕获,触发当前方法的Spy.onThrows规则
2.由于Spy.onReturn和Spy.onThrows,都会触发EventListenerHandler.handleOnEnd ,导致

  • EventProcessor.Process 中执行两次出栈,造成栈错位
  • 如果父级调用是最顶层调用的话,多出一次出栈,会导致父级调用的栈缺失,触发EventListenerHandler.handleOnEnd中栈错位逻辑,流程快速返回,无法调用EventListener

3.使用了AdviceAdapterListener,在处理RETURN和IMMEDIATELY_RETURN 事件过程中,也会导致OpStack opStack 多次出栈,造成OpStack栈的错位

解决方案:
第一种方案: 同级调用中的Spy.onThrows 忽略 同级的Spy.onReturn中通过ProcessControlException 的THROWS_IMMEDIATELY事件抛出的异常,同时注意对齐栈的操作
第二种方案:ProcessControlException 的THROWS_IMMEDIATELY事件触发后,补齐EventProcessor.Process 和OpStack的栈

@z529192557
Copy link
Collaborator Author

相关问题 #123

@oldmanpushcart
Copy link
Collaborator

妈的,看得头都大

@z529192557
Copy link
Collaborator Author

妈的,看得头都大
简单点说:
方法A中的:Spy.spyMethodOnReturn中用ProcessControlException 强制抛出的异常,会被方法A中Spy.spyMethodOnThrows捕获,Spy.spyMethodOnReturn和Spy.spyMethodOnThrows都会导致方法返回出栈,原本A方法返回只需要一次出栈,现在Spy.spyMethodOnReturn出栈一次,Spy.spyMethodOnThrows又出栈一次,方法A的返回导致了EventProcessor.Process两次出栈,栈错位了

oldmanpushcart pushed a commit that referenced this issue Jan 9, 2020
@oldmanpushcart
Copy link
Collaborator

已经修复了,可以测试下哦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants