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

   # Description
   
   [CAMEL-27090](https://issues.apache.org/jira/browse/CAMEL-27090)
   
   When the session to the SMSC is lost, jsmpp calls the session state listener 
of `SmppConsumer` (and `SmppProducer`) on its own thread (the reader thread of 
the lost session). The listener closes the session and calls `reconnect()`, 
which runs the reconnect task on that thread: `BackgroundTask.run` schedules 
the attempts on the reconnect service and waits on the latch of the task. The 
task is built `withUnlimitedDuration()`, so the wait has no timeout; only an 
attempt that reconnects, or an attempt that sees the consumer stopping, counts 
the latch down.
   
   A stop while the reconnect waits for its next attempt (the SMSC is down; 
`initialReconnectDelay` and `reconnectDelay` are 5 s by default) calls 
`shutdownReconnectService`, and `shutdown()` cancels the scheduled attempts. No 
attempt runs any more, so the thread of the lost session waits for ever and the 
task stays in the task registry. A `reconnect()` that builds its task after the 
stop began schedules it on the shut down service, where it is dropped (the 
default `CallerRunsPolicy` discards a task once the pool is shut down), with 
the same result. Every route stop or context stop during an SMSC outage leaves 
one such thread.
   
   Found with a TLA+ model of the listener thread, the reconnect service thread 
and the stop (TLC). On main the invariant "after the stop, no thread waits on a 
reconnect latch that nothing can count down" is violated in 10 states: SMSC 
down, session lost, the listener thread waits for the first attempt, stop: 
shutdown cancels the attempt, the thread waits for ever. A first fix that only 
cancels the task in `doStop` is also violated (the listener thread publishes 
its task after the stop looked for it). The fix below holds (382 distinct 
states, 2 losses, the SMSC going down and up), and the witnesses for a 
reconnect that succeeds and for a stop during an outage after a reconnect are 
reachable.
   
   Fix, in `SmppConsumer` and `SmppProducer`, as `PgEventConsumer` and 
`MasterConsumer` already do with their tasks:
   - `reconnect()` keeps the task in a volatile field, and runs it only if the 
service is not stopping;
   - `doStop` cancels the task in that field after the reconnect service is 
shut down, which counts the latch down and releases the waiting thread.
   
   `SmppUtils.newReconnectTask` now returns `BackgroundTask` instead of the 
`BlockingTask` interface, so that the task can be cancelled (source compatible 
for callers that assign it to a `BlockingTask`).
   
   jsmpp 3.0.2 was checked with javap: `PDUReaderWorker` calls the listener 
synchronously, and `PendingResponse.waitDone` fails the bind when the waiting 
thread is interrupted, so an attempt that the `shutdownNow` of the stop 
interrupts does not bind a new session after the stop. That case is not changed 
here.
   
   Tests: `SmppStopDuringReconnectTest` binds the real consumer and producer to 
an SMSC made with jsmpp's `SMPPServerSessionListener`, closes the server 
session and the listener (the SMSC goes down), waits until a thread waits in 
`reconnect()`, stops the route and waits until no thread is left in 
`reconnect()`. Without the change both tests fail after 10 seconds (the thread 
is still there); with the change the camel-smpp unit tests pass (175 run, 0 
failures, 4 skipped as before) and the new tests pass 20 times in one JVM 
(40/40).
   
   # 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-27090](https://issues.apache.org/jira/browse/CAMEL-27090).
   
   # 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 the core modules and camel-smpp with the formatter and 
import-sort plugins; no option, doc or generated file changes. 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