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

apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3f17403e2294acde2a4a92b60e4d2194f5bd85af
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Mon Nov 30 09:50:47 2026 +0100

    CAMEL-27774: camel-reactor - Fix lost completion of to() requests when the 
exchange completes early
    
    ReactorStreamsService.doRequest() built its result as
    Mono.create(sink -> addOnCompletion(...)).doOnSubscribe(subs -> 
consumer.process(...)).
    Reactor runs doOnSubscribe before the create callback, so the exchange was
    handed to the thread pool of the reactive-streams consumer before the
    completion callback was registered. When a pool thread completed the
    exchange first, the completion was lost, the Mono never emitted and
    callers blocked until their timeout (30 seconds in ReactorStreamsServiceTest
    testTo, testToWithExchange, testToFunctionWithExchange).
    
    Register the completion callback and process the exchange inside the
    Mono.create callback, in that order.
    
    Add a regression test that makes 2000 sequential to() calls, which reliably
    timed out before the fix.
    
    Found thanks to the flaky test ReactorStreamsServiceTest, see CAMEL-26778.
    
    Co-authored-by: IBM Bob <[email protected]>
---
 .../reactor/engine/ReactorStreamsService.java      | 34 ++++++++++++----------
 .../reactor/engine/ReactorStreamsServiceTest.java  | 15 ++++++++++
 2 files changed, 34 insertions(+), 15 deletions(-)

diff --git 
a/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
 
b/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
index 407dddcf8204..05427c04e645 100644
--- 
a/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
+++ 
b/components/camel-reactor/src/main/java/org/apache/camel/component/reactor/engine/ReactorStreamsService.java
@@ -314,23 +314,27 @@ final class ReactorStreamsService extends ServiceSupport 
implements CamelReactiv
             throw new IllegalStateException("No consumers attached to the 
stream " + name);
         }
 
-        return Mono.<Exchange> create(
-                sink -> data.getExchangeExtension().addOnCompletion(new 
Synchronization() {
-                    @Override
-                    public void onComplete(Exchange exchange) {
-                        sink.success(exchange);
+        return Mono.<Exchange> create(sink -> {
+            data.getExchangeExtension().addOnCompletion(new Synchronization() {
+                @Override
+                public void onComplete(Exchange exchange) {
+                    sink.success(exchange);
+                }
+
+                @Override
+                public void onFailure(Exchange exchange) {
+                    Throwable throwable = exchange.getException();
+                    if (throwable == null) {
+                        throwable = new IllegalStateException("Unknown 
Exception");
                     }
 
-                    @Override
-                    public void onFailure(Exchange exchange) {
-                        Throwable throwable = exchange.getException();
-                        if (throwable == null) {
-                            throwable = new IllegalStateException("Unknown 
Exception");
-                        }
+                    sink.error(throwable);
+                }
+            });
 
-                        sink.error(throwable);
-                    }
-                })).doOnSubscribe(
-                        subs -> consumer.process(data, 
ReactorStreamsConstants.EMPTY_ASYNC_CALLBACK));
+            // process the exchange once the completion callback is 
registered: the create callback runs after
+            // doOnSubscribe, so processing from there could complete the 
exchange before it is registered
+            consumer.process(data, 
ReactorStreamsConstants.EMPTY_ASYNC_CALLBACK);
+        });
     }
 }
diff --git 
a/components/camel-reactor/src/test/java/org/apache/camel/component/reactor/engine/ReactorStreamsServiceTest.java
 
b/components/camel-reactor/src/test/java/org/apache/camel/component/reactor/engine/ReactorStreamsServiceTest.java
index ec356e014f52..fbca59e0e4c3 100644
--- 
a/components/camel-reactor/src/test/java/org/apache/camel/component/reactor/engine/ReactorStreamsServiceTest.java
+++ 
b/components/camel-reactor/src/test/java/org/apache/camel/component/reactor/engine/ReactorStreamsServiceTest.java
@@ -37,6 +37,7 @@ import org.junit.jupiter.api.Test;
 import org.reactivestreams.Publisher;
 import reactor.core.Disposable;
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -335,6 +336,20 @@ public class ReactorStreamsServiceTest extends 
ReactorStreamsServiceTestSupport
         assertEquals(new TreeSet<>(Arrays.asList("Hello 1", "Hello 2", "Hello 
3")), new TreeSet<>(values));
     }
 
+    @Test
+    public void testToCompletesWhenRouteIsFasterThanSubscription() throws 
Exception {
+        context.start();
+
+        // the exchange is routed by another thread: its completion must not 
be missed when it is faster than the
+        // subscriber that requested it
+        for (int i = 0; i < 2000; i++) {
+            String value = Mono.from(crs.to("bean:hello", i, String.class))
+                    .block(Duration.ofSeconds(30));
+
+            assertEquals("Hello " + i, value);
+        }
+    }
+
     // ************************************************
     // subscriber
     // ************************************************

Reply via email to