[
https://issues.apache.org/jira/browse/FELIX-6879?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18153518#comment-18153518
]
Andreas Lemmer commented on FELIX-6879:
---------------------------------------
We hit this in production with Felix framework 8.0.0 as well (the code on
master is unchanged), so it is not specific to 7.0.5.
Setup: JasperReports runs in a wrapper bundle with "DynamicImport-Package: *",
through which it reaches the classes used in report expressions. Right after a
framework start, two reports filled concurrently race for the same
not-yet-wired package. One thread wires it, and the other gets null from
StatefulResolver.resolve(revision, pkgName) because isAllowedDynamicImport now
sees hasPackageSource. The class load then ends in a NoClassDefFoundError:
JRExpressionEvalException: ... $P\{contactPerson}.getIdentifier()
Caused by: java.lang.NoClassDefFoundError: <domain class reached via dynamic
import>
at <ReportName>_<id>.evaluate(...)
The consequence can be worse than a single failed load. Per JVMS 5.4.3, a
failed resolution of a symbolic reference is recorded, and every later attempt
fails with the same error without asking the class loader again. If the class
that holds the reference is cached, as JasperReports caches its compiled
expression evaluator classes, it stays broken until the framework restarts,
even though the dynamic wire exists by then. A minimal plain-JDK test (a loader
that throws ClassNotFoundException once and succeeds afterwards) shows that on
JDK 21: every call fails with the same NoClassDefFoundError.
The change in https://github.com/apache/felix-dev/pull/616 (returning
getImportedPackageSource(pkgName) when isAllowedDynamicImport returns false) is
what we would expect to fix it. +1 for getting it reviewed and released.
> Dynamic import race in StatefulResolver::resolve
> ------------------------------------------------
>
> Key: FELIX-6879
> URL: https://issues.apache.org/jira/browse/FELIX-6879
> Project: Felix
> Issue Type: Bug
> Components: Framework
> Affects Versions: framework-7.0.5
> Reporter: Joshua Zygmunt
> Priority: Minor
>
> There is a race condition in the StatefulResolver::resolve method. The method
> has the following signature: "BundleRevision resolve(BundleRevision revision,
> String pkgName)". This logic impacts
> BundleWiringImpl::findClassOrResourceByDelegation and results in an
> unnecessary ClassNotFoundException, which is how I found this issue.
> Near the beginning of the method it checks the return value of
> "isAllowedDynamicImport", if true the StatefulResolver will continue to
> resolve the pkgName for the BundleRevision, otherwise the method will soon
> return a null BundleRevision which can lead to a ClassNotFoundException
> depending on the caller of this method.
> "isAllowedDynamicImport" will return false if the package has already
> succeeded in being dynamically imported.
> If two threads enter this method at the same time, looking for the same
> pkgName, it is possible for one thread to return the successfully found
> BundleRevision and the other to return a null BundleRevision because
> "isAllowedDynamicImport" returned false because the other thread successfully
> found the bundle revision and dynamically imported the pkgName.
> In the existing code there is already a check to see if the pkgName was
> already successfully imported in the event that "isAllowedDynamicImport"
> returns true. To totally avoid the race condition it is necessary to do
> similar check in the event that "isAllowedDynamicImport" returns false. Below
> is the existing check that StatefulResolver current performs.
>
> // Double check to make sure that someone hasn't beaten us to
> // dynamically importing the package, which can happen if two
> // threads are racing to do so. If we have an existing wire,
> // then just return it instead.
> provider = ((BundleWiringImpl) revision.getWiring())
> .getImportedPackageSource(pkgName);
>
> This logic impacts the BundleWiringImpl::findClassOrResourceByDelegation as
> well. {*}I have seen this occur{*}. Broadly, this method call will attempt to
> find a class or resource by various means, like searching explicit imports
> through the "searchImports" method. If all measures fail it resorts to
> searching dynamic imports through the "searchDynamicImports" method. It is
> possible for two threads to enter findClassOrResourceByDelegation for the
> same pkgName, both threads call "searchImports" and do not find the pkgName
> and only one of them succeed in searching their dynamic imports because of
> the StatefulResolver logic. Subsequent calls to
> "findClassOrResourceByDelegation" succeed because "searchImports" returns a
> non null result because of the successful dynamic import.
>
>
>
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)