From d06ee68d7274832bb9c6d66cbd88fe7675fcc059 Mon Sep 17 00:00:00 2001 From: Pierre De Rop Date: Thu, 26 Nov 2026 22:20:00 -0600 Subject: [PATCH] FELIX-6894: Make Component.getServiceProperties() thread-safe Motivation: Component.getServiceProperties() can be called from any thread (e.g. the dm shell command). It reads the dependency events map, which is a plain HashMap modified by the component executor. If a propagated dependency is removed from the component meanwhile, getDependencyEvent() throws a NullPointerException. Modification: Use a ConcurrentHashMap for the dependency events, and make getDependencyEvent()/getDependencyEvents() return null/an empty set for a dependency which is no longer part of the component. Result: The service properties of a component can be safely computed from any thread, even while its dependencies are being removed. --- .../apache/felix/dm/impl/ComponentImpl.java | 11 +- ...X6894_ConcurrentServicePropertiesTest.java | 113 ++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 dependencymanager/org.apache.felix.dependencymanager/test/test/FELIX6894_ConcurrentServicePropertiesTest.java diff --git a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java index 5a44379682..11ddd31b38 100644 --- a/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java +++ b/dependencymanager/org.apache.felix.dependencymanager/src/org/apache/felix/dm/impl/ComponentImpl.java @@ -32,9 +32,9 @@ import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Dictionary; import java.util.Enumeration; -import java.util.HashMap; import java.util.Hashtable; import java.util.IdentityHashMap; import java.util.List; @@ -207,7 +207,7 @@ public class ComponentImpl implements Component, ComponentContext, ComponentDecl * Holds all the services of a given dependency context. Caution: the last entry in the skiplist is the highest * ranked service. */ - private final Map> m_dependencyEvents = new HashMap<>(); + private final Map> m_dependencyEvents = new ConcurrentHashMap<>(); /** * Flag used to check if this component has been added in a DependencyManager object. @@ -585,13 +585,16 @@ public void handleEvent(final DependencyContext dc, final EventType type, final @Override public Event getDependencyEvent(DependencyContext dc) { + // The dependency may have been concurrently removed from the component (we can be called from any thread). ConcurrentSkipListSet events = m_dependencyEvents.get(dc); - return events.size() > 0 ? events.last() : null; + return events != null && events.size() > 0 ? events.last() : null; } @Override public Set getDependencyEvents(DependencyContext dc) { - return m_dependencyEvents.get(dc); + // The dependency may have been concurrently removed from the component (we can be called from any thread). + Set events = m_dependencyEvents.get(dc); + return events != null ? events : Collections.emptySet(); } @Override diff --git a/dependencymanager/org.apache.felix.dependencymanager/test/test/FELIX6894_ConcurrentServicePropertiesTest.java b/dependencymanager/org.apache.felix.dependencymanager/test/test/FELIX6894_ConcurrentServicePropertiesTest.java new file mode 100644 index 0000000000..d205d8b5b3 --- /dev/null +++ b/dependencymanager/org.apache.felix.dependencymanager/test/test/FELIX6894_ConcurrentServicePropertiesTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package test; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.felix.dm.context.DependencyContext; +import org.apache.felix.dm.context.Event; +import org.apache.felix.dm.impl.ComponentImpl; +import org.junit.Assert; +import org.junit.Test; + +/** + * Checks that Component.getServiceProperties() can be called from a foreign thread while + * a propagated dependency is concurrently removed from the component. + */ +public class FELIX6894_ConcurrentServicePropertiesTest { + + /** + * A propagated dependency which, like the real service dependency, reads its current + * dependency events from the component when computing its properties. + * Once armed, it blocks inside getProperties() until the test has removed it from the component. + */ + static class PropagatedDependency extends SimpleServiceDependency { + volatile boolean m_armed; + final CountDownLatch m_inGetProperties = new CountDownLatch(1); + final CountDownLatch m_removed = new CountDownLatch(1); + volatile Event m_event; + volatile Set m_events; + + @Override + public DependencyContext createCopy() { + return new PropagatedDependency(); + } + + @SuppressWarnings("unchecked") + @Override + public Dictionary getProperties() { + if (m_armed) { + m_inGetProperties.countDown(); + try { + m_removed.await(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + m_event = m_component.getDependencyEvent(this); + m_events = m_component.getDependencyEvents(this); + } + return (Dictionary) new Hashtable(); + } + } + + @Test + public void getServicePropertiesWhileDependencyIsRemoved() throws Exception { + ComponentImpl c = new ComponentImpl(); + c.setImplementation(new Object()); + PropagatedDependency d = new PropagatedDependency(); + d.setRequired(false); + d.setPropagate(true); + c.add(d); + c.start(); + d.add(new EventImpl(1)); // makes the dependency available, so its properties are propagated + Assert.assertTrue(d.isAvailable()); + + d.m_armed = true; + AtomicReference error = new AtomicReference<>(); + Thread reader = new Thread(() -> { + try { + c.getServiceProperties(); + } catch (Throwable t) { + error.set(t); + } + }, "getServiceProperties"); + reader.start(); + + // The reader thread is now computing the service properties, and still sees the dependency. + Assert.assertTrue(d.m_inGetProperties.await(5, TimeUnit.SECONDS)); + c.remove(d); + d.m_removed.countDown(); + reader.join(5000); + + Assert.assertFalse("reader thread did not complete", reader.isAlive()); + if (error.get() != null) { + throw new AssertionError("getServiceProperties() failed", error.get()); + } + Assert.assertNull(d.m_event); + Assert.assertNotNull(d.m_events); + Assert.assertTrue(d.m_events.isEmpty()); + + c.stop(); + } +}