This is an automated email from the ASF dual-hosted git repository. timothyjward pushed a commit to branch ARIES-2255_coalesce_close_race in repository https://gitbox.apache.org/repos/asf/aries-component-dsl.git
commit 1aa9b5c1da6f47361faa4385b7ac7a55dd771747 Author: Tim Ward <[email protected]> AuthorDate: Tue Dec 1 13:23:38 2026 +0000 ARIES-2255: Fix NPE closing a coalesced program during an update The terminator read a result that the deferred publication had not set yet, so a close during a configuration update threw and left the downstream program open. Signed-off-by: Tim Ward <[email protected]> --- .../component/dsl/internal/CoalesceOSGiImpl.java | 28 ++++- .../dsl/internal/CoalesceOSGiImplTest.java | 113 +++++++++++++++++++++ 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImpl.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImpl.java index 218f908..f262200 100644 --- a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImpl.java +++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImpl.java @@ -51,6 +51,9 @@ public class CoalesceOSGiImpl<T> extends OSGiImpl<T> { publishers[i] = t -> { AtomicReference<OSGiResult> result = new AtomicReference<>(); + // The publication can be deferred, so termination may + // be requested before the result exists + AtomicBoolean terminated = new AtomicBoolean(); synchronized (initialized) { atomicInteger.incrementAndGet(); @@ -66,13 +69,32 @@ public class CoalesceOSGiImpl<T> extends OSGiImpl<T> { } } - UpdateSupport.deferPublication( - () -> result.set(op.publish(t))); + UpdateSupport.deferPublication(() -> { + if (terminated.get()) { + return; + } + + OSGiResult published = op.publish(t); + + result.set(published); + + if (terminated.get() && + result.compareAndSet(published, null)) { + + published.close(); + } + }); } return () -> UpdateSupport.deferTermination(() -> { synchronized (initialized) { - result.get().close(); + terminated.set(true); + + OSGiResult toClose = result.getAndSet(null); + + if (toClose != null) { + toClose.close(); + } int current = atomicInteger.decrementAndGet(); diff --git a/component-dsl/src/test/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImplTest.java b/component-dsl/src/test/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImplTest.java new file mode 100644 index 0000000..d5dcb62 --- /dev/null +++ b/component-dsl/src/test/java/org/apache/aries/component/dsl/internal/CoalesceOSGiImplTest.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 org.apache.aries.component.dsl.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.aries.component.dsl.OSGi; +import org.apache.aries.component.dsl.OSGiResult; +import org.apache.aries.component.dsl.Publisher; +import org.junit.Test; + +public class CoalesceOSGiImplTest { + + /** + * ARIES-2255: a publication made during an update (as a configuration + * event does) is deferred until the update ends. Terminating it while the + * deferred publication is still running must neither fail nor leak the + * downstream result. + */ + @Test + public void testTerminateWhileDeferredPublicationIsRunning() throws Exception { + CountDownLatch publishing = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + AtomicInteger added = new AtomicInteger(); + AtomicInteger removed = new AtomicInteger(); + AtomicReference<Publisher<? super String>> publisher = new AtomicReference<>(); + AtomicReference<OSGiResult> publication = new AtomicReference<>(); + + OSGi<String> source = OSGiImpl.create((bundleContext, op) -> { + publisher.set(op); + + return () -> {}; + }); + + OSGi<String> program = OSGi.coalesce(source, OSGi.just("fallback")).effects( + x -> { + added.incrementAndGet(); + + // Only the publication from the source is held up + if ("update".equals(x)) { + publishing.countDown(); + await(release); + } + }, + x -> removed.incrementAndGet()); + + ExecutorService updates = Executors.newSingleThreadExecutor(); + ExecutorService closer = Executors.newSingleThreadExecutor(); + + try { + // The program is running on the fallback when the update arrives + OSGiResult result = program.run(null); + + Future<?> update = updates.submit(() -> UpdateSupport.runUpdate( + () -> publication.set(publisher.get().publish("update")))); + + assertTrue(publishing.await(10, TimeUnit.SECONDS)); + + // Terminate while the downstream publication has not returned + Future<?> terminating = closer.submit(() -> publication.get().close()); + + // Fails with a NullPointerException without the fix + terminating.get(10, TimeUnit.SECONDS); + + release.countDown(); + + update.get(10, TimeUnit.SECONDS); + + result.close(); + } + finally { + release.countDown(); + updates.shutdownNow(); + closer.shutdownNow(); + } + + // Every result that was published has been closed, none leaked + assertEquals(added.get(), removed.get()); + } + + private static void await(CountDownLatch latch) { + try { + latch.await(10, TimeUnit.SECONDS); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + +}
