davsclaus commented on code in PR #26573:
URL: https://github.com/apache/camel/pull/26573#discussion_r4044935355
##########
core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java:
##########
@@ -878,8 +878,11 @@ public static void replaceMessage(Exchange exchange,
Message newMessage, boolean
exchange.setIn(newMessage);
}
- // need to de-reference old from the exchange so it can be GC
- if (old instanceof MessageSupport messageSupport) {
+ // need to de-reference old from the exchange so it can be GC, but
only if the exchange no longer
+ // references it: with outOnly and no OUT message yet, old is the
(untouched) IN message and detaching it
+ // would leave IN without an exchange reference. Use hasOut() before
getOut() as getOut() lazily creates OUT.
+ if (old != exchange.getIn() && !(exchange.hasOut() && old ==
exchange.getOut())
Review Comment:
The condition is correct for all four `(outOnly, hasOut)` combinations, but
it re-derives "is `old` still referenced?" after the exchange has been mutated.
The only case where `old` stays on the exchange is `outOnly &&
!exchange.hasOut()`, so capturing that once *before* the `setOut`/`setIn` reads
more directly and avoids the `hasOut()`-before-`getOut()` subtlety:
```java
Message old = exchange.getMessage();
// with outOnly and no OUT yet, old is the IN message which stays on the
exchange untouched
boolean oldStaysReferenced = outOnly && !exchange.hasOut();
if (outOnly || exchange.hasOut()) {
exchange.setOut(newMessage);
} else {
exchange.setIn(newMessage);
}
// need to de-reference old from the exchange so it can be GC
if (!oldStaysReferenced && old instanceof MessageSupport messageSupport) {
messageSupport.setExchange(null);
}
```
Purely optional — the current version works.
--
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]