Affects Version/s: framework-2.0.3
Fix Version/s: None
Environment:Linux, JDK 6.
In http://hg.netbeans.org/core-main/raw-file/default/core.netigso/test/unit/src/org/netbeans/core/osgi/ActivatorTest.java I have some unit tests which repeatedly launch Felix, start some bundles, shut down, and repeat. On occasion - more reproducibly if calls to System.gc() and System.runFinalization() are inserted into ActivatorTest.setUp - I get errors like these (though the test still passes):
Here some code in a bundle has registered a special ReferenceQueue and is doing some minor cleanup of recently finalized objects. Unfortunately running this code block can trigger fresh class loading and JarContent throws an ISE when trying to load from the now-closed JAR file. The situation is less likely to come up in a real app than in a unit test but still possible - in case a bundle is dynamically unloaded, or some cleanup tasks happen to run during JVM shutdown.
The timing of class loading is not easily predictable: it will occur any time a section of code is run for the first time. Even in the absence of apparent threads, it is very hard to guarantee that no class loading will take place after code ceases to be called externally, since overridden finalize() methods and JVM shutdown hooks can be called passively at any time. The code in this example could disable its RQ upon BundleActivator.stop if it were originally written for use inside OSGi, but it is not.
I have come up with a patch to JarFileX which lets it load classes from nominally closed JARs on an emergency basis. (This was implemented years ago in the NetBeans module system.) To make it safer for the original JAR to be recreated or deleted, especially on Windows with its mandatory file locks, a temporary copy is made.
(Safest would be to copy the original JAR eagerly in close(), but this would impose a huge performance penalty. Instead, the JAR is copied on demand only in cases where an ISE would otherwise be thrown. It is possible for the JAR to be modified/deleted after close() but before the next class load, in which case the ISE will still occur; similarly if a SecurityManager prevents the copying, etc.)
It is not clear to me from the OSGi spec whether it is permissible for the bundle class loader to continue to function after framework shutdown (or generally after a bundle moves into an unresolved state). The spec seems to say that Bundle.loadClass should throw ISE, but this is different from performing implicit class loading at the VM's request as part of running already-loaded code. For what it's worth, 4.4.10 does say "all old exports must remain available for existing bundles and future resolves until the refreshPackages method is called or the Framework is restarted". While more permissive behavior is very useful for situations like these, if it contradicts the spec, I might suggest one or both of the following:
1. Enable emergency loading only with an optional Felix framework property. Then, for example, unit tests which knew they would be starting and stopping code which potentially left behind live threads or finalizer queues etc. could set the property to avoid printing such exceptions.
2. At least report when close() was called to assist the user in debugging the problem.