shihchengwei-lab commented on PR #606:
URL: https://github.com/apache/felix-dev/pull/606#issuecomment-7215186129

   The new early return in `handleAdded` can leak a service acquired by an 
`ADDED` event that was queued while an instance-bound dependency was stopping.
   
   I reproduced this on `86d55800e388710eebbeafb7f05b362dcefc248b`. A second 
thread submits a `ServiceEventImpl` while `stop()` is still running. After 
`stop()` returns, this PR removes the dependency's entry from 
`m_dependencyEvents`; the queued `handleAdded` then sees `null` and returns 
without closing the event. `BundleContext.ungetService(reference)` is never 
called.
   
   Adding `e.close()` before that return makes this reproducer and the three 
existing `FELIX6886_InstanceBoundDependencyLeakTest` cases pass.
   
   <details>
   <summary>Minimal reproduction</summary>
   
   ```java
   package 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:
   
   ```text
   Wanted but not invoked:
   bundleContext.ungetService(reference)
   ```
   
   </details>
   
   Could rejected incoming events be closed before these early returns? The 
same ownership pattern appears for `newEvent` in `handleSwapped` after the 
dependency entry is gone, although I only reproduced the `ADDED` path.
   
   This review, reproducer, and comment were prepared with AI assistance; I 
reviewed the evidence before posting.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to