Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DependencyContext, ConcurrentSkipListSet<Event>> m_dependencyEvents = new HashMap<>();
private final Map<DependencyContext, ConcurrentSkipListSet<Event>> m_dependencyEvents = new ConcurrentHashMap<>();

/**
* Flag used to check if this component has been added in a DependencyManager object.
Expand Down Expand Up @@ -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<Event> events = m_dependencyEvents.get(dc);
return events.size() > 0 ? events.last() : null;
return events != null && events.size() > 0 ? events.last() : null;
}

@Override
public Set<Event> 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<Event> events = m_dependencyEvents.get(dc);
return events != null ? events : Collections.emptySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Event> m_events;

@Override
public DependencyContext createCopy() {
return new PropagatedDependency();
}

@SuppressWarnings("unchecked")
@Override
public <K, V> Dictionary<K, V> 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<K, V>) new Hashtable<String, Object>();
}
}

@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<Throwable> 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();
}
}
Loading