allthingssecurity opened a new pull request, #30823:
URL: https://github.com/apache/camel/pull/30823

   # Description
   
   [CAMEL-27530](https://issues.apache.org/jira/browse/CAMEL-27530)
   
   With `idleTimeout` set, `MllpTcpClientProducer.checkConnection()` schedules 
an idle check (`run()`) on `idleTimeoutExecutor` for every new connection, and 
`run()` schedules itself again as long as the connection it finds in the 
`socket` field is open and not idle. Nothing cancelled a check that was already 
scheduled, and `run()` did not look at which connection it was scheduled for. 
Three things followed from that:
   
   - **Every reconnect added one more check chain.** When the producer 
reconnects inside `process()` (the receiver, a firewall or a load balancer 
dropped the connection, so the next write or ack read fails and the producer 
reconnects and resends), the check of the old connection is still pending. When 
it runs, it finds the new connection open and reschedules itself, next to the 
check scheduled for the new connection. Each later reconnect adds one more, and 
the chains only end once the connection has been idle for a whole 
`idleTimeout`. Against a receiver that drops the connection after each message, 
that is one more chain per message, each running at least once per 
`idleTimeout` and taking the same lock as `process()`. In a reproduction 
against the real producer, 6 messages left 6 pending checks, and 11 runs of the 
check were needed to close the one idle connection. No message is lost or 
duplicated by this.
   - **A check survived a stop and start.** `doStop` called `shutdown()` on the 
executor, and a `ScheduledThreadPoolExecutor` still runs its delayed tasks 
after `shutdown()` by default. After `start()`, the old check ran on the old 
executor's thread, found the connection of the new start open and rescheduled 
itself on the executor of the new start: two chains after one restart.
   - **`process()` on a stopped producer opened a connection that nothing 
closed.** `process()`, `run()`, `start()` and `stop()` share the `BaseService` 
lock, so a route thread that called `process()` while `stop()` held the lock 
ran on the stopped producer afterwards. It opened a new connection, assigned 
it, and then failed with a `NullPointerException` (wrapped in a 
`RuntimeException`) because `doStop` had set the executor to null. The 
connection stayed open while the producer was stopped, and after the next 
`start()` the producer used it with no idle check, so `idleTimeout` never 
closed it. Without `idleTimeout` the call sent the message on the stopped 
producer and left its connection open. This needs a `process()` call that 
arrives while the producer stops: a forced shutdown after the graceful timeout, 
a redelivery during a stop, or a producer cache that stops a producer another 
thread still uses.
   
   The three are in one PR because they are the same class and one small 
change: the field that holds the pending check is what makes the stop/start 
case and the reconnect case go away, and the lifecycle check in `process()` is 
what keeps that field and the executor consistent with the connection after a 
stop.
   
   ## The change
   
   - The producer keeps the future of its idle check in a field, 
`idleTimeoutCheck`, guarded by the producer lock like the `socket` field. Every 
schedule goes through `scheduleIdleTimeoutCheck(delay)`, which cancels the 
pending check before it stores the new one: in `checkConnection()` for a new 
connection, and in `run()` when the check reschedules itself. So a producer has 
at most one pending check.
   - `run()` cancels too, not only `checkConnection()`. A check that the 
executor thread has already taken from its queue cannot be stopped by 
`cancel()`: it waits for the lock while `process()` reconnects (or while the 
producer stops and starts again), then runs. Cancelling the pending check 
before rescheduling makes that late run replace the check of the new connection 
instead of adding a second one. With the cancel only in `checkConnection()`, 
the two new tests for this case fail with 2 pending checks.
   - `doStop` cancels the pending check and calls `shutdownNow()` instead of 
`shutdown()`. The executor is created by `doStart` for this producer only 
(`Executors.newSingleThreadScheduledExecutor`, not the 
`ExecutorServiceManager`), so nothing else uses it. A check that is running at 
that moment waits for the lock that `stop()` holds; `shutdownNow()` does not 
end it (the lock wait is not interruptible), and when it runs it either finds 
the connection closed by `doStop`, or it replaces the check of the next start 
as above.
   - `process()` fails the exchange with a `RejectedExecutionException` ("The 
MLLP producer for mllp://host:port is not started") when the producer is not 
started, right after it takes the lock and before it connects. `NettyProducer` 
does the same.
   
   ## Tests
   
   New `MllpTcpClientProducerIdleCheckTest` (package-private, JUnit assertions 
as in the module, Awaitility for the waits, no sleeps). After `start()` the 
test replaces the producer's idle executor with a manual one, so the test 
decides when a check runs; its cancel and shutdown behave like the JDK executor 
the producer uses (a started check runs to its end when cancelled, `shutdown()` 
keeps the delayed checks, `shutdownNow()` drops them). The receiver is the 
module's `MllpServerResource`. The lock is reached through a test subclass of 
the producer.
   
   - `reconnectReplacesTheIdleCheckOfThePreviousConnection`: the receiver 
closes the connection instead of acknowledging the second message of each 
connection, so each of 5 sends after the first reconnects and resends inside 
`process()`. After each message one check is pending; when the connection is 
idle, that check closes it and none is left.
   - `idleCheckThatRunsWhileProcessReconnectsDoesNotAddAChain`: the check of 
the first connection waits for the lock while `process()` reconnects; then it 
runs, finds the new connection open and reschedules itself. One check is 
pending, and the connection that was just used is not closed.
   - `idleCheckClosingTheConnectionRacesASend`: the connection is idle, the 
check and a send wait for the lock together. In either order the message is 
acknowledged and one check is pending. This one is a control and passes on main.
   - `idleCheckOfThePreviousStartDoesNotRunAfterARestart`: after `stop()` the 
executor of the first start keeps no check to run; after `start()` and a send, 
one check is pending.
   - `idleCheckWaitingForTheLockDuringAStopDoesNotAddAChainAfterTheRestart`: 
the executor of the first start has taken its check, which waits for the lock 
while the producer stops, starts and sends on a new connection. One check is 
pending afterwards.
   - `processWaitingForTheLockWhileTheProducerStopsOpensNoConnection` (with 
`idleTimeout`) and `...WithoutIdleTimeout`: a `process()` call waits for the 
lock while the producer stops. It does not throw, the exchange has a 
`RejectedExecutionException`, and no new connection was opened. With 
`idleTimeout`, the next start then sends on a new connection with its check.
   
   On main, 6 of the 7 fail (the control passes). With the cancel only in 
`checkConnection()` (no cancel in `run()`), the two tests where a check waits 
for the lock fail. With `doStop` as on main, 
`idleCheckOfThePreviousStartDoesNotRunAfterARestart` fails. Without the check 
in `process()`, the two `processWaiting...` tests fail.
   
   With the change: the new class 20 times in one JVM, 140/140. camel-mllp unit 
tests: 397 run, 3 skipped, 0 failures, except one run of 
`MllpTcpServerConsumerRequiredEndOfDataWithoutValidationTest` that could not 
bind its port while another build ran; that class then passed 3 times out of 3. 
`MllpIdleTimeoutStrategyTest` is timing sensitive: run 10 times in one JVM (3 
test methods, 30 test runs), it had 5 failures on main (close 3, reset 2) and 4 
with the change (close 2, default 2), spread over its methods; it passed in the 
Maven runs. The formatter and import-sort plugins made no changes.
   
   Model: found with a TLA+ model of the producer (`process()` with its 
reconnect and resend paths, the idle check, stop and start, a receiver that 
drops connections). For this change I split the idle check into "the executor 
takes the task" and "it runs under the lock", because a cancel between the two 
does not stop it. I did not run the model checker on that version for this PR; 
the earlier version, with the fix as a cancel on reconnect and on stop, held 
"at most one pending check", "no open connection after a stop" and "an open 
connection of a started producer has a check" with 2 and 3 route threads and 
one stop and start.
   
   Not changed:
   
   - The JMX operations `closeConnection()` and `resetConnection()` still run 
without the lock, as before.
   - The idle time is still measured from the start of the last `process()` 
call, so a call that takes longer than `idleTimeout` (a slow ack) is followed 
by an idle close of its connection and a reconnect on the next message.
   - The producer still resends once within the same `process()` call after a 
write or ack read failure, so a receiver that processed the message before the 
connection failed gets it twice (at-least-once delivery, as before).
   - No upgrade guide entry: a `process()` call can only reach a stopped 
producer in the race above, and there it now fails instead of leaving a 
connection open.
   
   Backport: the same code is on camel-4.22.x, camel-4.18.x and camel-4.14.x.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27530](https://issues.apache.org/jira/browse/CAMEL-27530).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built and tested the affected module, including the formatter and 
import-sort plugins. I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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