sofastack / sofa-boot

SOFABoot is a framework that enhances Spring Boot and fully compatible with it, provides readiness check, class isolation, etc.
https://www.sofastack.tech/sofa-boot/docs/Home
Apache License 2.0
4.97k stars 1.27k forks source link

Error java.lang.LinkageError occurred when use sofa-boot(v3.2.0) and sofa-ark(v1.0.0) together #478

Closed ggndnn closed 5 years ago

ggndnn commented 5 years ago

Based on sofa-boot v3.2.0 and sofa-ark 1.0.0, when I started application, I got a error:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving interface method "com.alipay.sofa.runtime.spi.component.SofaRuntimeManager.registerShutdownAware(Lcom/alipay/sofa/runtime/spi/spring/RuntimeShutdownAware;)V" the class loader (instance of com/alipay/sofa/ark/container/service/classloader/BizClassLoader) of the current class, com/alipay/sofa/runtime/spring/RuntimeShutdownAwarePostProcessor, and the class loader (instance of com/alipay/sofa/ark/container/service/classloader/PluginClassLoader) for the method's defining class, com/alipay/sofa/runtime/spi/component/SofaRuntimeManager, have different Class objects for the type com/alipay/sofa/runtime/spi/spring/RuntimeShutdownAware used in the signature

After analysis, I found it's caused by that runtime-sofa-boot-plugin did not export com.alipay.sofa.runtime.spi.spring.RuntimeShutdownAware. Because RuntimeShutdownAwarePostProcessor was loaded by bizClassLoader, SofaRuntimeManager was loaded by pluginClassLoader, they both imported RuntimeShutdownAware, so two RuntimeShutdownAware classes ware loaded separately by bizClassLoader and pluginClassLoader, when RuntimeShutdownAwarePostProcessor tried to call SofaRuntimeManager's register method with a RuntimeShutdownAware instance, this error occurred.

Sofa-boot v3.1.5 did not have such issue, because RuntimeShutdownAwarePostProcessor was not added then (replacement of ApplicationShutdownCallbackPostProcessor?), and no other classes used RuntimeShutdownAware.

By the way, the class comment on RuntimeShutdownAwarePostProcessor is since 2.5.0, it's a little misleading, should it be since 3.2.0?

I think no harm done to export com.alipay.sofa.runtime.spi.spring.RuntimeShutdownAware by runtime-sofa-boot-plugin, that prevent bizClassLoader from loading this class. If you agree what I analyzed, I will submit a PR to fix this.

glmapper commented 5 years ago

that is, you can provide PR to resolve this problem