paulrutter commented on PR #598:
URL: https://github.com/apache/felix-dev/pull/598#issuecomment-7110416332

   Hi Pierre,
   
   This PR fixes the `ServiceRegistryCacheManager` init, and the new test shows 
it. While testing parallel startup I found a possible second race. It is 
separate from this PR, so I report it here for your opinion.
   
   ## The possible race
   
   - `MultiPropertyFilterIndex.getPermutations` sorts the array it receives, in 
place: `Arrays.sort(values, String.CASE_INSENSITIVE_ORDER)`.
   - For the `objectClass` property, that array is the live array of the 
registration. The framework stores it as `m_classes` 
(`ServiceRegistrationImpl`) and `getProperty` returns it with no copy.
   - Adapter components share one array between all children. `FilterComponent` 
copies the interfaces once into `m_serviceInterfaces`. `AdapterServiceImpl` 
then passes that same array to `setInterface` for every child. So all children 
register with the same `objectClass` array.
   - Two threads that sort the same unsorted array at the same time can 
duplicate one name and lose another. A small test with two threads and a 4-name 
array corrupted 2 of 300,000 arrays.
   - A wrong array makes the index file the service under the wrong key. A 
dependency that uses the index then never finds the service.
   
   ## Why I suspect it
   
   In a parallel startup with 20 tenant-scoped copies of one adapter component, 
the application did not finish starting in 1 of about 150 runs. There was no 
error and no stuck thread. Every component that depends on the service from the 
shared array stayed unstarted, for all 20 copies at the same time. A shared 
array explains why all copies fail together.
   
   ## What is proven and what is not
   
   - Proven: the in-place sort, and the shared array (code path above). This is 
the same in `main` and in 8.0.0.
   - Proven: a new unit test, `objectClassArrayOfReferenceIsNotModified`, fails 
without the fix and passes with it.
   - Not proven: that this race caused the stalled startup. I will compare the 
`objectClass` arrays of the next stalled run with a good run.
   
   ## Proposed fix
   
   Sort a copy in `getPermutations`:
   
   ```java
   List<MultiPropertyKey> getPermutations(String key, String[] propertyValues) {
       List<MultiPropertyKey> results = new ArrayList<>();
       // Sort a copy. The array of a service property can be the live array of 
the registration.
       String[] values = propertyValues.clone();
       Arrays.sort(values, String.CASE_INSENSITIVE_ORDER);
       ...
   ```
   
   The change is 3 lines plus the test. `createKeys` also sorts the property 
keys in place. That one is safe today, because the framework returns a copy of 
the keys.
   
   Do you prefer the fix in this PR, or in its own issue and PR?


-- 
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