Paul Rütter created FELIX-6880:
----------------------------------

             Summary: DM filter index sorts the shared objectClass array of a 
service registration in place
                 Key: FELIX-6880
                 URL: https://issues.apache.org/jira/browse/FELIX-6880
             Project: Felix
          Issue Type: Bug
          Components: Dependency Manager
    Affects Versions: org.apache.felix.dependencymanager-5.0.0
            Reporter: Paul Rütter


MultiPropertyFilterIndex.getPermutations(String key, String[] values) sorts the 
array that it receives:

    Arrays.sort(values, String.CASE_INSENSITIVE_ORDER);

createKeys(ServiceReference) reads that array with reference.getProperty(key). 
For the objectClass property, the framework returns the live array of the 
registration. ServiceRegistrationImpl stores the array as m_classes, puts it in 
the property map, and getProperty returns it with no copy.

The array can also be shared between registrations. FilterComponent copies the 
service interfaces once. AdapterServiceImpl then passes that same array to 
setInterface for every child component. All children register with the same 
objectClass array.

When threads register and track services in parallel, two threads can sort the 
same unsorted array at the same time. A concurrent sort can duplicate one name 
and lose another. The index then files the service under the wrong key, and a 
dependency that uses the index does not find the service.

Reproduction of the corruption (plain Java, no OSGi): two threads call 
Arrays.sort(array, String.CASE_INSENSITIVE_ORDER) on the same unsorted 4-name 
array at the same time, 300000 rounds. 2 arrays came out with a duplicate name.

Observed (not proven to be this race): in parallel startup with 20 copies of 
one adapter component, the application did not finish starting in 1 of about 
150 runs. No error, no stuck thread. All components that depend on the service 
with the shared array stayed unstarted.

Fix: sort a copy of the array in getPermutations. A unit test checks that the 
array of the service reference stays unchanged.

createKeys also sorts the property keys in place. That is safe today, because 
the framework returns a copy of the keys.



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

Reply via email to