Joshua Zygmunt created FELIX-6879:
-------------------------------------
Summary: 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
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)