gnodet-bot commented on code in PR #30001:
URL: https://github.com/apache/camel/pull/30001#discussion_r4577594678


##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/SendProcessor.java:
##########
@@ -264,13 +251,87 @@ private boolean sendUsingProducer(
             }
             return sync;
         } catch (Exception throwable) {
+            if (sent != null && !sent.compareAndSet(false, true)) {
+                // the producer called the callback and then threw: the 
exchange is already continued (maybe on
+                // another thread), so it is not completed a second time and 
the exception is not set on it
+                LOG.warn("The producer of {} threw an exception after it 
completed the exchange. This exception is ignored.",
+                        destination, throwable);
+                return sent.doneSync;
+            }
             exchange.setException(throwable);
-            callback.done(true);
+            if (sent != null) {
+                // the producer threw before it called the callback: complete 
the send as if it had called it
+                sent.complete(true);
+            } else {
+                // the callback of the caller was given to the producer as is 
(no object per send on this path), so
+                // whether the producer called it before it threw is only 
known by the caller
+                callback.done(true);
+            }
         }
 
         return true;
     }
 
+    /**
+     * The callback of a send in {@link #sendUsingProducer}, when the send 
needs one: stores the result in the variable,
+     * restores the exchange pattern, emits the sent event and calls the 
callback of the caller, once (also when the
+     * producer calls its callback and then throws; a further call of the 
callback by the producer is only passed on).
+     * One object per send, as the lambda it replaces.
+     */
+    private final class SentCallback extends AtomicBoolean implements 
AsyncCallback {
+        private final Exchange target;
+        private final ExchangePattern existingPattern;
+        private final Object originalBody;
+        private final Map<String, Object> originalHeaders;
+        private final StopWatch watch;
+        private final AsyncCallback callback;
+        private volatile boolean doneSync;
+
+        private SentCallback(Exchange target, ExchangePattern existingPattern, 
Object originalBody,
+                             Map<String, Object> originalHeaders, StopWatch 
watch, AsyncCallback callback) {
+            this.target = target;
+            this.existingPattern = existingPattern;
+            this.originalBody = originalBody;
+            this.originalHeaders = originalHeaders;
+            this.watch = watch;
+            this.callback = callback;
+        }
+
+        @Override
+        public void done(boolean doneSync) {
+            if (compareAndSet(false, true)) {

Review Comment:
   ⚠️ **JMM ordering gap — `SentCallback.done()`**
   
   `this.doneSync = doneSync` is written inside `complete()` *after* the 
winning CAS (line 302). The catch block in `sendUsingProducer` reads 
`sent.doneSync` (line ~259) after a failed CAS. The failed CAS establishes 
happens-before only with the winning CAS, not with subsequent writes on the 
winning thread — so there is a race window where the losing thread reads the 
default `false` instead of the actual value.
   
   ```suggestion
           public void done(boolean doneSync) {
               this.doneSync = doneSync;
               if (compareAndSet(false, true)) {
                   complete(doneSync);
               } else {
   ```
   
   Moving the volatile write before the CAS ensures it is visible to any thread 
that observes the CAS result (winning or losing).



##########
core/camel-support/src/main/java/org/apache/camel/support/cache/DefaultProducerCache.java:
##########
@@ -349,21 +352,79 @@ protected boolean asyncDispatchExchange(
         exchange.setProperty(ExchangePropertyKey.TO_ENDPOINT, 
endpoint.getEndpointUri());
 
         // send the exchange using the processor; an exception thrown by the 
producer is handled by doInAsyncProducer,
-        // which knows whether the producer called the callback before it threw
-        EventNotifierCallback notifier = null;
-        if (eventNotifierEnabled && 
camelContext.getCamelContextExtension().isEventNotificationApplicable()) {
-            notifier = new EventNotifierCallback(callback, exchange, endpoint);
-            callback = notifier;
+        // which knows whether the producer called the callback before it 
threw. doInAsyncProducer also emits the
+        // sending and sent events of the send
+        return sharedInternalProcessor.process(exchange, callback, producer, 
resultProcessor);
+    }
+
+    /**
+     * The producer of a synchronous {@link #send}: calls the producer, and 
calls the callback once, also when the
+     * producer throws after it called the callback (the exception is then 
logged, and not set on the exchange, as in
+     * {@link #doInAsyncProducer}) or before it (the exception is then set on 
the exchange, as when the producer sets
+     * it). One object per send. An {@link Error} is thrown on as before.
+     */
+    private static final class SyncSend extends AtomicBoolean implements 
AsyncProcessor, AsyncCallback {
+        private final Endpoint endpoint;
+        private final AsyncProcessor producer;
+        private AsyncCallback callback;
+        private volatile boolean doneSync;
+
+        private SyncSend(Endpoint endpoint, AsyncProcessor producer) {
+            this.endpoint = endpoint;
+            this.producer = producer;
         }
-        try {
-            // invoke the asynchronous method
-            return sharedInternalProcessor.process(exchange, callback, 
producer, resultProcessor);
-        } catch (Throwable e) {
-            if (notifier != null) {
-                // pair the sending event with a sent event, unless the 
callback did already
-                notifier.notifySentOnce();
+
+        @Override
+        public boolean process(Exchange exchange, AsyncCallback callback) {
+            this.callback = callback;
+            try {
+                return producer.process(exchange, this);
+            } catch (RuntimeException e) {
+                if (!compareAndSet(false, true)) {
+                    // the producer called the callback and then threw: the 
send is already completed
+                    LOG.warn("The producer of {} threw an exception after it 
completed the exchange. This exception is"
+                             + " ignored.",
+                            endpoint, e);
+                    return doneSync;
+                }
+                exchange.setException(e);
+                callback.done(true);
+                return true;
+            }
+        }
+
+        @Override
+        public void done(boolean doneSync) {
+            if (compareAndSet(false, true)) {

Review Comment:
   ⚠️ **JMM ordering gap — `SyncSend.done()`**
   
   Same pattern as `SentCallback`: `this.doneSync = doneSync` is written at 
line 399, *after* `compareAndSet(false, true)` at line 398. The `process()` 
catch block reads `doneSync` (line ~388) after its failed CAS (line ~383). The 
happens-before chain from failed CAS → winning CAS does not extend to the 
subsequent `doneSync` write.
   
   ```suggestion
           public void done(boolean doneSync) {
               this.doneSync = doneSync;
               if (compareAndSet(false, true)) {
                   callback.done(doneSync);
   ```
   
   Moving the volatile write before the CAS closes the race window.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to