sofastack / sofa-ark

SOFAArk is a light-weight,java based classloader isolation framework.
https://www.sofastack.tech/projects/sofa-boot/sofa-ark-readme/
Apache License 2.0
1.57k stars 500 forks source link

【Bug】开源组件库中使用Reflection.getCallerClass导致ClassLoader混乱的问题 #696

Closed zhengchangqing closed 12 months ago

zhengchangqing commented 1 year ago

Describe the bug

在 Java 中,Reflection API 的 Reflection.getCallerClass() 方法用于获取调用者的类。但是,getCallerClass() 方法的实现依赖于调用栈信息。当存在多个类加载器时,不同类加载器加载的类可能会存在于调用栈中,这可能导致 getCallerClass() 方法返回的类与期望不一致。

Steps to reproduce

使用log4j2日志库时,获取的LoggerContext一直都是宿主应用的Context,调用栈可见: Log4J2LoggingSystem#beforeInitialize()

Environment

zhengchangqing commented 1 year ago

@lvjing2

hustchaya commented 1 year ago

能截图一下详细的不同的classloader导致的问题么?我这边断点基座和模块之间没有这个问题

zhengchangqing commented 1 year ago

getCallerClass

Steps to reproduce中所述,第三方组件库(log4j组件库等)获取ClassLoader不是通过线程上下文获取的,而是通过它的调用类获取,导致获取到的ClassLoader不是当前BizClassLoader,Debug可以看到完整的调用链。

hustchaya commented 1 year ago

因为我这边用arthas调用getLoggerContext,拿到的context都是一样的,断点拿到的是基座的classloader(模块日志正常打印),所以想要你给下实际拿到的值和期望拿到的值对照,以及这个对你的模块日志打印造成了什么样的影响?

github-actions[bot] commented 12 months ago

This issue has been automatically marked as stale because it has not had recent activity in the last 30 days. It will be closed in the next 7 days unless it is tagged (pinned, good first issue or help wanted) or other activity occurs. Thank you for your contributions.

lvjing2 commented 12 months ago

getCallerClass

Steps to reproduce中所述,第三方组件库(log4j组件库等)获取ClassLoader不是通过线程上下文获取的,而是通过它的调用类获取,导致获取到的ClassLoader不是当前BizClassLoader,Debug可以看到完整的调用链。

log4j2 getLoggerContext 的问题通过我们提供的 adapter 可以解决,已经提供对应的 samples 工程 https://github.com/sofastack/sofa-serverless/tree/master/samples/springboot-samples/logging https://sofaserverless.gitee.io/docs/contribution-guidelines/runtime/logj42/