javaee / hk2

A light-weight and dynamic dependency injection framework
https://javaee.github.io/hk2
Other
112 stars 83 forks source link

OSGi service leak #374

Closed muter closed 6 years ago

muter commented 7 years ago

Method org.jvnet.hk2.osgiadapter.HK2Main.HK2ServiceTrackerCustomizer#addingService(...) wraps a registered osgi service with an instance of AbstractActiveDescriptorand commits this descriptor into a global ServiceLocator.

When service is removed tracker attempts to remove descriptor from ServiceLocator using filter that checks equality of a removal candidate with a descriptor used to track service. (See HKMain.java#395)

This filter, however, will always return false, causing descriptor to be retained in a ServiceLocator preventing removed services (and so classloaders of uninstalled bundles) from being collected by GC.

The filter is checking an equality of its parameter with the service descriptor. Call to matches method in a ServiceLocatorImpl passes an instance of SystemDescriptor as a parameter (see ServiceLocatorImpl.java#L368). SystemDescriptor overrides equals method so that it returns false for objects that are not instances of SystemDescriptor (see SystemDescriptor.java#L929). But descriptor created in addingService method can not be instance of SystemDescriptor, since it is instance of AbstractActiveDescriptor.

Issue was reproduced on version 2.4.0 but still affects master, as far as I can tell.