Pierre De Rop created FELIX-6883:
------------------------------------

             Summary:  Service returned before injection/start in parallel mode
                 Key: FELIX-6883
                 URL: https://issues.apache.org/jira/browse/FELIX-6883
             Project: Felix
          Issue Type: Bug
          Components: Dependency Manager
            Reporter: Pierre De Rop


When a component with *PROTOTYPE* (or *BUNDLE*) scope is managed by a 
DependencyManager configured with a thread pool (parallel mode, via a 
{{ComponentExecutorFactory}}), the service instance returned to the consumer by 
{{getService()}} may not be initialized yet:
* its dependencies are not injected;
* its {{init}}/{{start}} callbacks have not been called.

These steps are completed later, asynchronously, in the thread pool. As a 
consequence, a consumer may receive an object whose injected fields are still 
{{null}}, leading to {{NullPointerException}}s or inconsistent behavior. The 
result is timing-dependent.

The same issue can also occur in serial mode, more rarely, when the prototype 
component queue is being executed by another thread at the time 
{{getService()}} is called.

h3. Example (DM API only)

{code:java}
// Parallel mode: a ComponentExecutorFactory service is registered and
// returns a thread pool, e.g. new ForkJoinPool(4)

public interface Service {}
public interface Dependency {}

public class ServiceImpl implements Service {
    volatile Dependency m_dependency; // injected by DM
    volatile boolean m_started;

    void start() { m_started = true; }
}

// Provider
DependencyManager dm = ...;
dm.add(dm.createComponent()
    .setInterface(Dependency.class.getName(), null)
    .setImplementation(new Dependency() {}));

dm.add(dm.createComponent()
    .setScope(ServiceScope.PROTOTYPE)
    .setInterface(Service.class.getName(), null)
    .setImplementation(ServiceImpl.class)
    
.add(dm.createServiceDependency().setService(Dependency.class).setRequired(true)));

// Consumer
ServiceReference<Service> ref = context.getServiceReference(Service.class);
ServiceObjects<Service> objects = context.getServiceObjects(ref);
ServiceImpl s = (ServiceImpl) objects.getService();
// s.m_dependency == null and s.m_started == false
{code}

In an integration test calling {{getService()}} 100 times in parallel mode, all 
100 returned instances were not yet injected and started.

h3. Expected behavior

The instance returned by {{getService()}} for a *PROTOTYPE* or *BUNDLE* scoped 
component must be fully initialized (required dependencies injected, 
{{init}}/{{start}} callbacks invoked), whether or not a thread pool is used. If 
the instance cannot be fully started, no partially initialized object should be 
returned to the consumer.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to