Repository navigation
Conversation
Motivation: When a component instance is destroyed, the dependencies added from its init() callback (instance-bound dependencies) are removed and stopped, but ComponentImpl keeps their entry in its dependency events map. Each destroy/init cycle leaks a dependency context and its events, and the services held by these events are never released. Modification: ComponentImpl now removes the entry of each instance-bound dependency it removes and closes its events. Events sent late by a removed dependency (for example when its tracker is closed) are ignored. A unit test reproduces the leak. Result: Removing an instance-bound dependency leaves no reference to it or to its events, as with Component.remove(Dependency).
|
The new early return in I reproduced this on Adding Minimal reproductionpackage test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.apache.felix.dm.Component;
import org.apache.felix.dm.context.ComponentContext;
import org.apache.felix.dm.impl.ComponentImpl;
import org.apache.felix.dm.impl.ServiceEventImpl;
import org.junit.Assert;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class FELIX6886_LateAddedServiceTest {
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void lateAddedServiceQueuedDuringStopIsReleased() throws Exception {
BundleContext bundleContext = mock(BundleContext.class);
ComponentContext componentContext = mock(ComponentContext.class);
ServiceReference reference = mock(ServiceReference.class);
when(componentContext.getBundleContext()).thenReturn(bundleContext);
ServiceEventImpl lateEvent =
new ServiceEventImpl(componentContext, reference, new Object());
SimpleServiceDependency required = new SimpleServiceDependency();
required.setRequired(true);
SimpleServiceDependency lateDependency = new SimpleServiceDependency() {
@Override
public void stop() {
super.stop();
Thread sender = new Thread(() -> add(lateEvent));
sender.start();
try {
sender.join(10_000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError(e);
}
if (sender.isAlive()) {
throw new AssertionError("late ADDED event was not queued");
}
}
};
lateDependency.setRequired(false);
ComponentImpl component = new ComponentImpl();
component.setImplementation(new Object() {
@SuppressWarnings("unused")
void init(Component comp) {
comp.add(lateDependency);
}
});
component.add(required);
component.start();
EventImpl requiredEvent = new EventImpl(1);
required.add(requiredEvent);
required.remove(requiredEvent);
Assert.assertNull(component.getDependencyEvents(lateDependency));
verify(bundleContext).ungetService(reference);
component.stop();
}
}On the PR head, the final verification fails with: Could rejected incoming events be closed before these early returns? The same ownership pattern appears for This review, reproducer, and comment were prepared with AI assistance; I reviewed the evidence before posting. |
Motivation: A late ADDED or SWAPPED event sent by an instance-bound dependency while it is being stopped is ignored once the dependency has been removed from the component. The service held by this event must still be released. Modification: Add a unit test where the late ADDED event is followed by the REMOVED event a service tracker sends for every service it has added, and an integration test using the real service tracker for a late ADDED event and a late SWAPPED event. Result: The tests check that the services obtained for these late events are released, and that handling the late events does not fail.
|
Thanks a lot for the careful review and the reproducer, much appreciated! You're right that the early return in Tracker contractAs far as I understand the DM tracker, every event for which
TestsI added the following tests in commit 22f4f3f
I may well be missing something here, though. If you think there is a path where the real tracker delivers an Next stepThat said, I still need to think about it a bit more: closing the rejected events in those early returns might still be a cheap defensive measure (for example for custom Thanks again! |
|
Thanks for adding the tests with the real tracker. My reproducer missed the matching REMOVED event. |
|
I'm the one who should thank you! Your review was very much appreciated. And please don't hesitate to reach out if you have any further concerns about this PR. |
Motivation:
When a component instance is destroyed, the dependencies added from its init() callback (instance-bound dependencies) are removed and stopped, but ComponentImpl keeps their entry in its dependency events map. Each destroy/init cycle leaks a dependency context and its events, and the services held by these events are never released.
Modification:
ComponentImpl now removes the entry of each instance-bound dependency it removes and closes its events. Events sent late by a removed dependency (for example when its tracker is closed) are ignored. A unit test reproduces the leak.
Result:
Removing an instance-bound dependency leaves no reference to it or to its events, as with Component.remove(Dependency).