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

   # Description
   
   [CAMEL-27492](https://issues.apache.org/jira/browse/CAMEL-27492)
   
   Two ways a camel-smpp consumer or producer ends up started with no working 
session, and nothing reconnects it.
   
   **1. A reconnect request could be thrown away.** When jsmpp reports a lost 
session, the listener closes the session and calls `reconnect()`, which is 
guarded by `tryLock()`: if another thread holds the lock, the request was only 
logged ("could not acquire a lock for creating the session ...") and dropped. 
The thread that reported the previous lost session holds the lock while it 
waits for the reconnect task, and still holds it for a moment after the task 
bound the new session. If the SMSC drops the new session in that window (an 
SMSC that closes right after its `bind_resp`: a restart, overload, "too many 
binds"), the new session's listener clears the session and its request is 
dropped. The consumer stays `Started` with no session, or with the closed one 
when the listener ran before the reconnect thread set the field. The producer 
had the same window around every holder of its `connectLock` (the reconnect, 
the eager start, a lazy session creation), and then every exchange fails wit
 h "Lost connection ... and yet not reconnected". The only trace is a WARN line.
   
   **2. A reconnect attempt of a stopped start closed the session of the next 
start.** A stop waits 1 s for the reconnect service and then calls 
`shutdownNow()`, which does not interrupt a blocking connect (its timeout is 
`enquireLinkTimer`, and a proxy CONNECT has no read timeout). After a stop and 
a start of the same instance (suspend and resume, a route restart), the old 
attempt fails late, and its catch block called `closeSession()`, which closes 
the session in the field: the session of the new start. Its listener had been 
removed first, so nothing reconnected it.
   
   Fix, in `SmppConsumer` and `SmppProducer`:
   - A reconnect request is never lost. The listener sets a 
`reconnectRequested` flag before it tries the lock; the thread that holds the 
lock checks the flag again when its reconnect is done and runs another one if 
the session is still closed (a request whose session was already replaced needs 
nothing).
   - Only the loss of the session in use asks for a reconnect. The listener 
closes the session that reported, and clears the field and reconnects only when 
the field still holds that session, instead of closing whatever session is in 
the field. A session that was never set or was replaced already asks for 
nothing: a bind that failed (jsmpp reports that close on its reader thread when 
the SMSC closes the connection after its `bind_resp`, 21 of 30 binds in a probe 
against a jsmpp SMSC that does so), or a listener of an earlier start.
   - A new session is set only while it is still bound, under the lock the 
listener takes. A session the SMSC drops between `connectAndBind` and the set 
fails the start, the lazy session creation or the reconnect attempt (which 
tries again after `reconnectDelay`), as a failed bind does.
   - A reconnect belongs to the start it was requested in. Every stop 
increments a generation; a reconnect task of an older generation ends at its 
next attempt, an attempt that binds after its start was stopped closes its new 
session instead of setting it, and a failed attempt no longer closes the 
session in the field. The session field and the generation change together 
under a small lock, and the field is `volatile`.
   - The producer runs the reconnect task under its own lock, and takes 
`connectLock` only to set the new session. So a start or a lazy session 
creation that is in progress finishes first and keeps its session (the attempt 
then closes its own), and a lazy producer can create its session while a 
reconnect waits for its next attempt; before, such an exchange failed with 
"Lost connection ... and yet not reconnected" until the reconnect succeeded.
   
   This keeps what CAMEL-27090 added: a stop still cancels the reconnect task 
and releases the thread that waits for it, and a stop and start in place 
creates the reconnect service again. CAMEL-17859 was an earlier report of a 
reconnect lost to `tryLock()`.
   
   So a reconnect that gives up after `maxReconnect` attempts is not started 
again by the binds it saw rejected, on whatever thread jsmpp reports them. (The 
first version of this change relied on the CAMEL-27491 fix for that, which only 
covers a close reported on the binding thread.)
   
   This builds on CAMEL-27491 (#30815, on main), a bind rejected while the 
consumer or producer starts no longer runs the reconnect on the starting 
thread. As there, a state change with no source (jsmpp always passes the 
session) still closes the session in use and reconnects: the listener takes it 
as the loss of the session in the field.
   
   Not changed here (I will file follow-up tickets):
   - when the reconnect gives up after `maxReconnect` attempts, the consumer 
stays started without a session and only a WARN line tells (camel-smpp has no 
health check);
   - jsmpp quirk seen while testing: when the SMSC closes right after its 
`bind_resp`, jsmpp can read the end of the connection before it handles the 
`bind_resp`, and the bind then waits 60 s (the fixed timeout of 
`connectAndBind` without a timeout argument) before it fails and the next 
attempt runs.
   
   Tests: new `SmppReconnectRaceTest` (8 tests, package-private), against an 
SMSC running in the test JVM (jsmpp's `SMPPServerSessionListener`, as in 
`SmppStopDuringReconnectTest`):
   - `consumerReconnectsASessionLostBeforeTheReconnectReleasedItsLock`, 
`producer...`: a delegating `TaskManagerRegistry` runs a hook on the thread 
that waited for the reconnect task, after the task bound session 2 and before 
that thread releases the lock; the hook makes the SMSC drop session 2 and waits 
until jsmpp's reader thread of session 2 has reported it. Session 3 is bound 
and works (a `deliver_sm` reaches the consumer, the producer sends);
   - `consumerReconnectsANewSessionLostBeforeItIsSet`, `producer...`: a session 
subclass runs a hook right after `connectAndBind` of session 2 returns on the 
reconnect thread, before the session is set; the SMSC drops session 2 there. 
Session 3 is bound and works;
   - `consumerStaleReconnectAttemptDoesNotCloseTheSessionOfTheNextStart`, 
`producer...`: a reconnect attempt is held in an HTTP CONNECT proxy, the 
consumer (producer) is stopped and started, then the proxy answers the old 
attempt with 502; the session of the new start still works and no other bind 
happens;
   - 
`consumerReconnectEndsAfterMaxReconnectWhenTheRejectedBindsAreReportedOnAnotherThread`,
 `producer...`: `maxReconnect=2`, the SMSC drops session 1 and rejects every 
bind after it, and a session subclass reports the close of a rejected bind on 
another thread before `connectAndBind` throws, as jsmpp's reader thread does; 
exactly 2 attempts are made and no further bind arrives within 1 s.
   
   The first six fail with the current consumer and producer (no third bind 
within 10 s, or the restarted session is closed). The two 
`...ReconnectEndsAfterMaxReconnect...` tests pass with the current code too, 
which gives up after `maxReconnect` as it should: they fail with the first 
version of this change (a further bind within 1 s), and pin that the request 
flag is not set by a rejected bind. With the change the camel-smpp unit tests 
pass on current main (191 run, 0 failures, 4 skipped as before), and 
`SmppReconnectRaceTest` with `SmppStopDuringReconnectTest` passed 20 of 20 runs 
(240 of 240 tests) before the rebase and 5 of 5 runs after it, together with 
`SmppBindRejectedTest` of CAMEL-27491. The listener tests of CAMEL-27491 in 
`SmppConsumerTest` and `SmppProducerTest` (session as the source, and a `null` 
source) pass unchanged.
   
   A TLA+ model of the reconnect found both: its "a started consumer that is 
quiet has a live session" property fails on the current code (lost request; 
stale attempt). The model has a switch for the first version of this fix 
(consumer only); I have not run its exhaustive check with the fix yet.
   
   Backport: the fix builds on CAMEL-27090 (main only so far).
   
   # 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-27492](https://issues.apache.org/jira/browse/CAMEL-27492).
   
   # 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 camel-smpp and the core modules with the formatter and 
import-sort plugins and ran the camel-smpp unit tests. I did not run the full 
root build. No generated file changes.)
   
   # 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