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

   # Description
   
   [CAMEL-27491](https://issues.apache.org/jira/browse/CAMEL-27491)
   
   When the SMSC rejects the bind of a consumer or an eager producer while it 
starts (wrong credentials, `ESME_RALYBND`, throttling, no `bind_resp` in time), 
the start never returned with the default `maxReconnect`, and a stop from 
another thread could not end it.
   
   How: jsmpp closes a session whose bind fails on the thread that called 
`connectAndBind`, and that close calls the session state listeners on the same 
thread before `connectAndBind` throws. The Camel listener is registered before 
the bind, so it ran on the starting thread: it saw a lost session and ran the 
whole reconnect task there, waiting for it with no time limit while holding the 
lifecycle lock of the service. So:
   
   - with the default `maxReconnect` (`Integer.MAX_VALUE`) the route (or 
`CamelContext`) start hangs as long as the SMSC keeps rejecting;
   - `stop()` from another thread (route controller, shutdown) waits for the 
same lifecycle lock, so the cancellation added by CAMEL-27090 cannot run either;
   - once the SMSC accepts again, the reconnect inside `start()` binds a 
session, and then `connectAndBind` throws anyway: the start fails and the stop 
that follows unbinds the session it just made.
   
   A lazy producer (`lazySessionCreation=true`) had the same problem on the 
route thread: `process()` ran the reconnect task inside the lazy session 
creation (its `connectLock` is reentrant), so the exchange was held until the 
SMSC accepted or the producer was stopped.
   
   When the SMSC closes the connection right after its negative `bind_resp`, 
jsmpp's reader thread often sees the end of the connection first and reports 
the close on that thread instead (21 of 30 binds against a jsmpp SMSC in the 
test JVM that closes after rejecting). The listener then runs the reconnect on 
the reader thread, as before, not on the starting thread: the start is not held 
by it, and the stop that follows the failed start cancels it (CAMEL-27090). 
jsmpp can then also drop the `bind_resp` itself, and the bind fails only after 
its fixed 60 s wait.
   
   Fix: the consumer and the producer remember, per thread, the session whose 
`connectAndBind` runs on that thread (a `ThreadLocal` set around the call), and 
the listener ignores a close that is reported on that thread for that session. 
That only happens when the bind fails, and then `connectAndBind` throws to its 
caller, which handles it. (The previous state of the session cannot tell a 
failed bind: jsmpp 3.0.2 marks the session bound as soon as a `bind_resp` 
arrives, also a negative one, and closes it afterwards.)
   
   - `doStart()` of the consumer or the eager producer: the start fails with 
the bind error as soon as `connectAndBind` throws, as it already does when the 
SMSC refuses the TCP connection: at once for a negative `bind_resp`, after 
jsmpp's fixed 60 s bind wait (`connectAndBind` without a timeout argument, 
which Camel uses) when the SMSC does not answer the bind;
   - the lazy producer: the exchange fails with the bind error and the next 
exchange tries again, as for a refused connection;
   - a reconnect attempt: it fails, and the reconnect task tries again after 
`reconnectDelay`, up to `maxReconnect` attempts, as before. (Before, the 
listener also ran on the reconnect thread for each rejected attempt and only 
logged that it could not get the lock.)
   
   A session that is lost after it was bound is reconnected as before.
   
   Why fail the start instead of retrying in the background: the options 
describe reconnecting after the connection was lost (`initialReconnectDelay`, 
`reconnectDelay`), `lazySessionCreation` exists so that a producer can start 
while the SMSC is not available, and a refused connection at start already 
fails the start. CAMEL-6616 (the producer start stuck in a reconnect loop on a 
negative bind response) asked for a start that gives up after a number of 
retries; its fix added `maxReconnect`, which bounds the loop, but with the 
default the start kept waiting while the SMSC rejected. Before this change the 
start failed in the end in every case (when `maxReconnect` ran out, or when the 
SMSC accepted: a retry that bound still ended in a failed start); it now fails 
without retrying. A user who wants the route to keep trying at startup can use 
the supervising route controller, which retries a route whose start failed with 
its back-off (before, the start did not return, so it never got to ret
 ry), or `lazySessionCreation` for a producer. No upgrade guide entry: no start 
succeeded through those retries before, the start only fails sooner.
   
   Not changed here (found with the same analysis; I will file follow-up 
tickets):
   - a session that is lost while another thread still holds the reconnect lock 
is not reconnected, and a reconnect attempt that outlives a stop can close the 
session of the next start (a sibling ticket, its fix is in a separate PR that 
also carries the listener change of this one, word for word, so the two merge 
in either order);
   - 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);
   - two narrow timing windows can leave a second bound session behind (the 
start writing its session after a reconnect wrote a newer one, and a listener 
of the previous start that runs only after a stop and a start); the sibling fix 
closes them too.
   
   Tests: new `SmppBindRejectedTest` (3 tests, package-private), against an 
SMSC running in the test JVM (jsmpp's `SMPPServerSessionListener`, as in 
`SmppStopDuringReconnectTest`) that rejects binds and then accepts them:
   - `consumerStartFailsAtOnceWhenTheSmscRejectsTheBind`: the start fails 
within 5 s with the bind error, no second bind arrives within 1 s 
(`reconnectDelay=100`), a stop from another thread returns; once the SMSC 
accepts, a new start binds and a `deliver_sm` reaches the consumer;
   - `producerStartFailsAtOnceWhenTheSmscRejectsTheBind`: the same for an eager 
producer; after the SMSC accepts, a new start binds and a message is sent;
   - `lazyProducerFailsTheExchangeAtOnceWhenTheSmscRejectsTheBind`: the 
exchange fails within 5 s with the bind error and the bind is not retried; once 
the SMSC accepts, the next exchange creates the session and is sent.
   
   The three tests fail with the current consumer and producer (the start, or 
the exchange, has not returned after 5 s). With the change the camel-smpp unit 
tests pass (181 run, 0 failures, 4 skipped as before), and 
`SmppBindRejectedTest` passes 20 of 20 runs. The existing listener tests in 
`SmppConsumerTest` and `SmppProducerTest` now pass the session as the source of 
the state change, as jsmpp does, instead of `null`.
   
   A TLA+ model of the reconnect (lifecycle thread, stopper, reconnect thread, 
jsmpp reader threads, route threads, an SMSC that rejects, refuses and drops) 
found this: its "the lifecycle lock is never held while waiting for a reconnect 
task" and "a route thread never waits for a reconnect task" properties fail on 
the current code. The model has a switch for this fix; I have not run its 
exhaustive check with the fix yet.
   
   Backport: the start has behaved like this since the reconnect task was 
introduced; the fix applies to older lines as is, if wanted.
   
   # 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-27491](https://issues.apache.org/jira/browse/CAMEL-27491).
   
   # 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