Pierre De Rop created FELIX-6894:
------------------------------------

             Summary: Component.getServiceProperties() is not safe outside the 
executor
                 Key: FELIX-6894
                 URL: https://issues.apache.org/jira/browse/FELIX-6894
             Project: Felix
          Issue Type: Bug
          Components: Dependency Manager
            Reporter: Pierre De Rop


{{ComponentImpl}} keeps the events of each dependency in a 
{{m_dependencyEvents}} map, which is a plain {{java.util.HashMap}}. The map is 
modified from the component executor (when a dependency is added to or removed 
from the component), but it is also read from arbitrary threads:

* {{Component.getServiceProperties()}} (public API) calls 
{{calculateServiceProperties()}}, which calls {{getProperties()}} on each 
propagated dependency, which calls {{ComponentContext.getDependencyEvent(dc)}};
* the {{dm}} shell command calls {{component.getServiceProperties()}} from the 
shell thread ({{DMCommand}}).

Because of this:

* reads from a foreign thread are not guaranteed to see a consistent state of 
the map while it is being modified by the executor;
* {{calculateServiceProperties()}} iterates over a snapshot of the component 
dependencies. If a dependency is removed from the component concurrently, the 
snapshot may still contain it while its entry has already been removed from 
{{m_dependencyEvents}}. {{getDependencyEvent(dc)}} then throws a 
{{NullPointerException}} ({{events.size()}} on a {{null}} set). 
{{getDependencyEvents(dc)}} returns {{null}} in the same situation.

h3. Example

{code:java}
Component c = dm.createComponent()
    .setInterface(Provider.class, null)
    .setImplementation(ProviderImpl.class);
ServiceDependency foo = dm.createServiceDependency()
    .setService(Foo.class)
    .setRequired(false)
    .setPropagate(true);
c.add(foo);
dm.add(c);

// Thread 1 (for example the "dm" shell command, or any code holding the 
Component)
while (true) {
    c.getServiceProperties();
}

// Thread 2
c.remove(foo);
{code}

When {{c.remove(foo)}} runs while thread 1 is computing the service properties, 
thread 1 may get a {{NullPointerException}} from 
{{ComponentImpl.getDependencyEvent}}.

h3. Expected behavior

{{Component.getServiceProperties()}} (and the other {{ComponentContext}} 
accessors used while computing it) can be called safely from any thread. A 
dependency that is being removed concurrently is simply ignored, and no 
exception is thrown.



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

Reply via email to