This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch SLING-13432
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-event.git

commit b2e088d6d3a4c1d6935e5e637209064b12e29676
Author: Joerg Hoh <[email protected]>
AuthorDate: Sun Nov 15 13:20:22 2026 +0100

    SLING-13432 add the fix
---
 .../sling/event/impl/jobs/JobConsumerManager.java  | 80 ++++++++++++++-------
 .../event/impl/jobs/JobConsumerManagerTest.java    | 81 ++++++++++++++++++++--
 2 files changed, 128 insertions(+), 33 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/event/impl/jobs/JobConsumerManager.java 
b/src/main/java/org/apache/sling/event/impl/jobs/JobConsumerManager.java
index ab7837d..721afd9 100644
--- a/src/main/java/org/apache/sling/event/impl/jobs/JobConsumerManager.java
+++ b/src/main/java/org/apache/sling/event/impl/jobs/JobConsumerManager.java
@@ -105,10 +105,11 @@ public class JobConsumerManager {
     /** The map with the consumers, keyed by topic, sorted by service ranking. 
*/
     private final Map<String, List<ConsumerInfo>> topicToConsumerMap = new 
HashMap<>();
 
-    /** ServiceRegistration for propagation. */
+    /** ServiceRegistration for propagation, guarded by topicToConsumerMap. */
     private ServiceRegistration<PropertyProvider> propagationService;
 
-    private String topics;
+    /** Discovery reads this value without acquiring the consumer-map lock. */
+    private volatile String topics;
 
     private TopicMatcher[] allowListMatchers;
 
@@ -179,18 +180,30 @@ public class JobConsumerManager {
 
     @Modified
     protected void modified(final BundleContext bc, final Config config, final 
DeprecatedConfig deprecatedConfig) {
-        final boolean wasEnabled = this.propagationService != null;
-        this.allowListMatchers = 
TopicMatcherHelper.buildMatchers(getAllowListConfig(config, deprecatedConfig));
-        this.denyListMatchers = 
TopicMatcherHelper.buildMatchers(getDenyListConfig(config, deprecatedConfig));
-
-        final boolean enable = this.allowListMatchers != null && 
this.denyListMatchers != TopicMatcherHelper.MATCH_ALL;
-        if (wasEnabled != enable) {
-            synchronized (this.topicToConsumerMap) {
-                this.calculateTopics(enable);
+        final TopicMatcher[] allowMatchers =
+                TopicMatcherHelper.buildMatchers(getAllowListConfig(config, 
deprecatedConfig));
+        final TopicMatcher[] denyMatchers =
+                TopicMatcherHelper.buildMatchers(getDenyListConfig(config, 
deprecatedConfig));
+        final boolean enable = allowMatchers != null && denyMatchers != 
TopicMatcherHelper.MATCH_ALL;
+        final boolean wasEnabled;
+        final ServiceRegistration<PropertyProvider> registration;
+        // Apply both filters and calculate topics atomically with respect to 
consumer binding/unbinding.
+        synchronized (this.topicToConsumerMap) {
+            registration = this.propagationService;
+            wasEnabled = registration != null;
+            this.allowListMatchers = allowMatchers;
+            this.denyListMatchers = denyMatchers;
+            this.calculateTopics(enable);
+            if (!enable) {
+                this.propagationService = null;
             }
+        }
+        if (wasEnabled != enable) {
             if (enable) {
                 logger.debug("Registering property provider with: {}", 
this.topics);
-                this.propagationService = bc.registerService(
+                // Registration invokes OSGi listeners synchronously. Keep the 
map lock free:
+                // a listener may wait for another thread to bind or unbind a 
consumer.
+                final ServiceRegistration<PropertyProvider> newRegistration = 
bc.registerService(
                         PropertyProvider.class,
                         new PropertyProvider() {
 
@@ -203,32 +216,40 @@ public class JobConsumerManager {
                             }
                         },
                         this.getRegistrationProperties());
+                synchronized (this.topicToConsumerMap) {
+                    this.propagationService = newRegistration;
+                    // Until registerService() returned, binding/unbinding saw 
a null handle,
+                    // could clear topics and could not notify Discovery. 
Rebuild from the current map.
+                    // Installing the handle under the same lock ensures later 
changes notify Discovery.
+                    this.calculateTopics(true);
+                }
+                // Always notify: Discovery may have cached an intermediate 
value, even if an
+                // add/remove sequence left the final consumer map identical 
to the initial one.
+                
newRegistration.setProperties(this.getRegistrationProperties());
             } else {
                 logger.debug("Unregistering property provider with");
-                this.propagationService.unregister();
-                this.propagationService = null;
+                registration.unregister();
             }
         } else if (enable) {
             // update properties
-            synchronized (this.topicToConsumerMap) {
-                this.calculateTopics(true);
-            }
             logger.debug("Updating property provider with: {}", this.topics);
-            
this.propagationService.setProperties(this.getRegistrationProperties());
+            registration.setProperties(this.getRegistrationProperties());
         }
     }
 
     @Deactivate
     protected void deactivate() {
-        if (this.propagationService != null) {
-            this.propagationService.unregister();
-            this.propagationService = null;
-        }
-        this.bundleContext = null;
+        final ServiceRegistration<PropertyProvider> registration;
         synchronized (this.topicToConsumerMap) {
+            registration = this.propagationService;
+            this.propagationService = null;
+            this.bundleContext = null;
             this.topicToConsumerMap.clear();
             this.listenerMap.clear();
         }
+        if (registration != null) {
+            registration.unregister();
+        }
     }
 
     /**
@@ -333,6 +354,7 @@ public class JobConsumerManager {
         if (topics != null && topics.length > 0) {
             final ConsumerInfo info = new ConsumerInfo(serviceReference, 
isConsumer);
             boolean changed = false;
+            final ServiceRegistration<PropertyProvider> registration;
             synchronized (this.topicToConsumerMap) {
                 for (final String t : topics) {
                     if (t != null) {
@@ -352,10 +374,13 @@ public class JobConsumerManager {
                 if (changed) {
                     this.calculateTopics(this.propagationService != null);
                 }
+                // Capture under the lock for visibility; notify outside it 
because OSGi callbacks
+                // are synchronous. If registration is pending, modified() 
reconciles after it returns.
+                registration = this.propagationService;
             }
-            if (changed && this.propagationService != null) {
+            if (changed && registration != null) {
                 logger.debug("Updating property provider with: {}", 
this.topics);
-                
this.propagationService.setProperties(this.getRegistrationProperties());
+                registration.setProperties(this.getRegistrationProperties());
             }
         }
     }
@@ -370,6 +395,7 @@ public class JobConsumerManager {
         if (topics != null && topics.length > 0) {
             final ConsumerInfo info = new ConsumerInfo(serviceReference, 
isConsumer);
             boolean changed = false;
+            final ServiceRegistration<PropertyProvider> registration;
             // Collect async callbacks to invoke outside the lock to avoid
             // lock-order inversion between topicToConsumerMap and 
QueueJobCache.cache
             final List<JobExecutionContext> asyncCallbacks = new ArrayList<>();
@@ -402,6 +428,8 @@ public class JobConsumerManager {
                 if (changed) {
                     this.calculateTopics(this.propagationService != null);
                 }
+                // As in bindService(), snapshot under the lock but invoke 
OSGi callbacks outside it.
+                registration = this.propagationService;
             }
             // notify listeners outside the lock
             for (final JobExecutionContext context : asyncCallbacks) {
@@ -414,9 +442,9 @@ public class JobConsumerManager {
                             ise);
                 }
             }
-            if (changed && this.propagationService != null) {
+            if (changed && registration != null) {
                 logger.debug("Updating property provider with: {}", 
this.topics);
-                
this.propagationService.setProperties(this.getRegistrationProperties());
+                registration.setProperties(this.getRegistrationProperties());
             }
         }
     }
diff --git 
a/src/test/java/org/apache/sling/event/impl/jobs/JobConsumerManagerTest.java 
b/src/test/java/org/apache/sling/event/impl/jobs/JobConsumerManagerTest.java
index c605d82..e70d87a 100644
--- a/src/test/java/org/apache/sling/event/impl/jobs/JobConsumerManagerTest.java
+++ b/src/test/java/org/apache/sling/event/impl/jobs/JobConsumerManagerTest.java
@@ -20,6 +20,8 @@ package org.apache.sling.event.impl.jobs;
 
 import java.lang.annotation.Annotation;
 
+import org.apache.sling.discovery.PropertyProvider;
+import org.apache.sling.event.impl.jobs.config.TopologyCapabilities;
 import org.apache.sling.event.jobs.consumer.JobConsumer;
 import org.apache.sling.event.jobs.consumer.JobExecutor;
 import org.junit.Test;
@@ -27,6 +29,7 @@ import org.mockito.Mockito;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -34,6 +37,14 @@ import static org.junit.Assert.assertNull;
 
 public class JobConsumerManagerTest {
 
+    private BundleContext getBundleContext() {
+        final BundleContext context = Mockito.mock(BundleContext.class);
+        Mockito.when(context.registerService(
+                        Mockito.eq(PropertyProvider.class), 
Mockito.any(PropertyProvider.class), Mockito.any()))
+                .thenAnswer(invocation -> 
Mockito.mock(ServiceRegistration.class));
+        return context;
+    }
+
     private JobConsumerManager.Config getDefaultConfig() {
         return new JobConsumerManager.Config() {
 
@@ -81,7 +92,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testSimpleMappingConsumer() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -101,7 +112,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testCategoryMappingConsumer() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -121,7 +132,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testSubCategoryMappingConsumer() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -141,7 +152,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testSimpleMappingExecutor() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -161,7 +172,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testCategoryMappingExecutor() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -181,7 +192,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testSubCategoryMappingExecutor() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -201,7 +212,7 @@ public class JobConsumerManagerTest {
 
     @Test
     public void testRanking() {
-        final BundleContext bc = Mockito.mock(BundleContext.class);
+        final BundleContext bc = getBundleContext();
         final JobConsumerManager jcs = new JobConsumerManager();
         jcs.activate(bc, getDefaultConfig(), getDeprecatedConfig());
 
@@ -247,4 +258,60 @@ public class JobConsumerManagerTest {
         jcs.unbindJobExecutor(ref3);
         assertEquals(jc4, jcs.getExecutor("a/b"));
     }
+
+    @Test
+    public void testLastExecutorRemovedDuringProviderRegistration() {
+        final BundleContext bc = getBundleContext();
+        final JobConsumerManager manager = new JobConsumerManager();
+        final ServiceReference<JobExecutor> existing = 
executorReference("a/existing", 1L);
+        final ServiceRegistration<?> registration = 
Mockito.mock(ServiceRegistration.class);
+        manager.bindJobExecutor(existing);
+        Mockito.doAnswer(invocation -> {
+                    final PropertyProvider provider = 
invocation.getArgument(1);
+                    assertEquals("a/existing", 
provider.getProperty(TopologyCapabilities.PROPERTY_TOPICS));
+                    manager.unbindJobExecutor(existing);
+                    return registration;
+                })
+                .when(bc)
+                .registerService(
+                        Mockito.eq(PropertyProvider.class), 
Mockito.any(PropertyProvider.class), Mockito.any());
+
+        manager.activate(bc, getDefaultConfig(), getDeprecatedConfig());
+
+        assertEquals("", manager.getTopics());
+        Mockito.verify(registration).setProperties(Mockito.any());
+    }
+
+    @Test
+    public void 
testTransientTopicChangeDuringProviderRegistrationIsPublished() {
+        final BundleContext bc = getBundleContext();
+        final JobConsumerManager manager = new JobConsumerManager();
+        final ServiceReference<JobExecutor> existing = 
executorReference("a/existing", 1L);
+        final ServiceReference<JobExecutor> arriving = 
executorReference("b/arriving", 2L);
+        final ServiceRegistration<?> registration = 
Mockito.mock(ServiceRegistration.class);
+        manager.bindJobExecutor(existing);
+        Mockito.doAnswer(invocation -> {
+                    final PropertyProvider provider = 
invocation.getArgument(1);
+                    assertEquals("a/existing", 
provider.getProperty(TopologyCapabilities.PROPERTY_TOPICS));
+                    manager.bindJobExecutor(arriving);
+                    manager.unbindJobExecutor(arriving);
+                    return registration;
+                })
+                .when(bc)
+                .registerService(
+                        Mockito.eq(PropertyProvider.class), 
Mockito.any(PropertyProvider.class), Mockito.any());
+
+        manager.activate(bc, getDefaultConfig(), getDeprecatedConfig());
+
+        assertEquals("a/existing", manager.getTopics());
+        Mockito.verify(registration).setProperties(Mockito.any());
+    }
+
+    @SuppressWarnings("unchecked")
+    private ServiceReference<JobExecutor> executorReference(final String 
topic, final long id) {
+        final ServiceReference<JobExecutor> reference = 
Mockito.mock(ServiceReference.class);
+        
Mockito.when(reference.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn(topic);
+        
Mockito.when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(id);
+        return reference;
+    }
 }

Reply via email to