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

   # Description
   
   [CAMEL-27332](https://issues.apache.org/jira/browse/CAMEL-27332)
   
   `MllpTcpServerConsumer.doStop` calls `stop()` on the consumer runnables that 
are in `consumerRunnables` at that moment, closes the accept thread's server 
socket and interrupts the bind thread. It does not stop the validation and 
consumer executors (only `doShutdown` does). A connection that the accept 
thread handed over just before the stop is still in its validation read (up to 
`min(500, receiveTimeout)` ms). When that read ends, `startConsumer` submits a 
new `TcpSocketConsumerRunnable` to the consumer executor without looking at the 
state of the consumer. The runnable sets `running = true` and serves the 
connection until the client goes away:
   
   - the stopped (or suspended by a route policy) route answers every message 
on it with `MSA|AE|...|RouteStoppingException`;
   - its thread and socket stay;
   - after a restart it is served next to the connections of the new start, so 
with `maxConcurrentConsumers=1` two connections are served.
   
   The model found a second way to the same state: `stop()` runs between 
`submit` and the first line of `run()`, and `run()` then sets `running` back to 
`true`. `running` was not volatile either, and it is written by the stopping 
thread.
   
   Each start also creates new executors (since CAMEL-22957), and the ones of 
the previous start were never shut down: their idle threads only end after the 
keep-alive time (`acceptTimeout`, 60 s by default, for the consumer pool), and 
every start leaves two executors registered with the `ExecutorServiceManager` 
until the CamelContext stops.
   
   The same kind of late start happens with `lenientBind=true`. The consumer 
then binds its port in a `TcpServerBindThread`, which retries every 
`bindRetryInterval` while the port is busy. `doStop` interrupts the accept 
thread when `acceptThread` is set, and then the bind thread. When the bind 
completes while the consumer stops (the port was busy at start and is freed 
while the route stops), the bind thread can call 
`consumer.startAcceptThread(serverSocket)` after `doStop` found no accept 
thread to stop. `startAcceptThread` does not check the state of the consumer, 
so an accept thread runs after the stop, the stopped route keeps listening on 
the port and accepts connections, and the port stays held, so starting the 
route again does not bind it. There is a second, smaller window: a bind attempt 
that is still running on the scheduler thread of the bind task when the 
interrupted bind thread gives up (`BackgroundTask.waitForTaskCompletion` 
cancels the task and returns `false`) can bind t
 he `ServerSocket`, and nothing closes it.
   
   This change:
   
   - `startConsumer` checks `isRunAllowed()` after it has put the runnable in 
`consumerRunnables`. When the consumer is stopping or stopped, it removes the 
runnable and resets the connection. The status is STOPPING before `doStop` 
runs, so a runnable is either in the map when `doStop` calls `stop()` on the 
runnables, or it is not started here.
   - `TcpSocketConsumerRunnable.running` is volatile and `true` from the start; 
`run()` no longer sets it, so a `stop()` before `run()` is not undone.
   - `doStop` shuts down the two executors of that start (`shutdown`, no wait): 
running runnables end as before, and a submit after that is rejected (the 
connection is reset).
   - `startAcceptThread` and the accept-thread step of `doStop` share a lock 
(`acceptThreadLock`). `startAcceptThread` closes the server socket instead of 
starting the accept thread when the consumer is not run-allowed. As above, the 
status is STOPPING before `doStop`, so either the accept thread is started 
before the stop reaches that step (and `doStop` interrupts it as before), or 
the bind thread sees the stop and closes the socket.
   - `TcpServerBindThread` closes the server socket when the bind task did not 
succeed (also after a bind timeout, where the unbound socket was left open 
before).
   
   A connection that was already served when the route stopped still ends when 
its runnable sees the stop, after its current read (up to `receiveTimeout`), as 
before. During that time, a restart can serve `maxConcurrentConsumers` new 
connections next to it; that is not changed here.
   
   Model: found with a TLA+ model of the MLLP consumer (accept, validation, 
consumer executor, lenient and non-lenient bind, stop and start). I extended it 
with the fix, on top of the maxConcurrentConsumers fix. On main, a runnable is 
submitted after the stop began and serves after the stop. With the fix, no 
runnable is submitted after the stop began, every runnable that serves after 
the stop has been told to stop (and ends), and the limit per executor and the 
map hold: 3 connections, a limit of 1, 3 starts: 3,443,294 distinct states; 
lenient bind, 3 connections, a limit of 2: 2,798,818. For the lenient bind part 
I changed the model so that a bound socket holds the port from the bind on and 
added the late bind of the second window. Without that part of the fix it finds 
an accept thread after the stop (56 distinct states) and an abandoned bound 
socket (27). With it no socket is left open after a stop, with 3 starts, 2 
connections, lenient and non-lenient bind (118,732 distinct states 
 lenient, 8,677 non-lenient). TLC and everystate agree on all of these.
   
   Tests: new `MllpTcpServerConsumerStopTest` (`maxConcurrentConsumers=1`, 
`receiveTimeout=2000`):
   
   - a client connects without sending and the route stops during the 
validation read: after the read, no consumer runnable runs, `consumerRunnables` 
is empty, and a message on the connection gets no answer;
   - the same, then the route is started again: a new connection is served, the 
old one is not, and one consumer runnable runs;
   - a connection served before the stop ends within the receive timeout 
(control, passes on main).
   
   On main the first two fail (a consumer runnable serves the connection after 
the stop; after the restart both connections are served); the control passes.
   
   New `MllpTcpServerConsumerLenientBindStopTest` (a consumer on a busy port 
with `lenientBind=true`):
   
   - the port is freed and the bind thread binds it, and the consumer stops 
before the accept thread starts (a test subclass holds `startAcceptThread` 
until the stop has returned, standing in for the scheduling of the bind 
thread): after the stop no accept thread must run, and the bound server socket 
must be closed. The latch the subclass waits on is also released when the test 
ends, so a failing run does not leave the bind thread waiting;
   - the consumer stops while the bind thread retries, then the port is freed: 
no accept thread must run (control).
   
   Without the lenient bind part of the change the first one fails (an accept 
thread runs after the stop, and the bound socket is open); the control passes.
   
   With the change the unit tests of camel-mllp pass: 385 run, 0 failures, 3 
skipped. The two new classes 20 times in one JVM: 100 run (5 tests x 20), 0 
failures. The formatter and import-sort plugins made no changes.
   
   Not changed:
   
   - `getLocalPort()` reads `acceptThread` without the lock, as before (it only 
reports the port).
   - The bind thread still logs "Failed to bind ... within timeout" when it was 
interrupted by the stop.
   
   # 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-27332](https://issues.apache.org/jira/browse/CAMEL-27332).
   
   # 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 modules, 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