shashank created CAMEL-27434:
--------------------------------
Summary: camel-salesforce - Pub/Sub consumer: a route stop during
a resubscribe leaves the subscription open, and a stream ended by the server is
not resubscribed
Key: CAMEL-27434
URL: https://issues.apache.org/jira/browse/CAMEL-27434
Project: Camel
Issue Type: Bug
Components: camel-salesforce
Reporter: shashank
Assignee: shashank
h3. What happens
The Pub/Sub API consumer resubscribes when its subscription stream fails.
CAMEL-24160 added a check of the client status before and after the back-off
sleep, so a stop during the sleep is handled. Three cases are still open in
{{PubSubApiClient}}:
*1. A route stop racing a resubscribe leaves a subscription open.* The
resubscribe passes the check after the sleep and {{asyncStub.subscribe}} starts
the new call. If the route is stopped now, {{doStop}} half-closes only the
streams already in {{observerMap}}, and the new one is not there yet.
{{channel.shutdown()}} lets existing calls go on and the result of
{{awaitTermination}} is ignored (no {{shutdownNow()}}). The resubscribe then
puts the stream into the map and sends the fetch request. The server delivers
events on a stream that nobody closes: they go to the stopped consumer, and
after the route is started again (a new client for the same consumer instance)
every event is processed twice. The channel does not terminate while that
stream is open.
*2. A stream ended by the server leaves the consumer without a subscription.*
{{FetchResponseObserver.onCompleted}} only removes the stream from the map: no
resubscribe and nothing reported. The route looks started but receives nothing
more. How often the Salesforce service (or a proxy) ends a subscribe stream
with status OK is not known, but when it happens the consumer stops silently.
*3. A failed call can replace the live stream in observerMap.* The map is keyed
by consumer and {{onError}} removes by key. If the call of a resubscribe fails
at once and its error callback resubscribes before the first thread reaches
{{observerMap.put}} (that thread must stall longer than the back-off), the
failed stream overwrites the live one, and a later normal stop leaves the live
stream open as in 1. Rare.
h3. How to reproduce
With an in-process fake Pub/Sub gRPC server and a client interceptor that holds
the resubscribing thread inside {{asyncStub.subscribe}}:
# the server sends one event and then {{UNAVAILABLE}}; {{client.stop()}} runs
while the resubscribe is held and returns; after release the new stream stays
open, the next event reaches the stopped consumer, and after a restart one
published event is processed twice;
# the server sends one event and then {{onCompleted()}}: no new subscription
after several back-off periods, the client is still started, the exception
handler is not called;
# the server rejects the second stream at once, the first resubscribe thread is
held until the third stream is open: after a normal stop the third stream is
still open and delivers to the stopped consumer.
Found with a TLA+ model of the client and consumer (gRPC callbacks, resubscribe
steps, stop and start, server errors and completion, crash, worker pool).
h3. Fix
* {{subscribe()}} holds a lock from a check of the client status until the
stream is stored and the fetch request is sent; {{doStop()}} takes the same
lock before it half-closes the streams in the map. The status is already
stopping then, so a resubscribe either stores its stream before {{doStop}}
closes the open streams or does not subscribe. The back-off sleep and the login
stay outside the lock.
* {{onError}}/{{onCompleted}} remove only their own stream ({{remove(consumer,
stream)}}); a call that already ended when {{subscribe()}} stores it is removed
again and gets no fetch request.
* {{onCompleted}} while the client is started resubscribes like {{onError}}
(same back-off, same replay id), with one WARN line.
* When {{awaitTermination}} times out, {{doStop}} calls
{{channel.shutdownNow()}} (INFO line); this also applies to the producer, which
uses the same client class.
* {{onError}} of a call that ends while the client is stopping (cancelled by
{{shutdownNow()}}, or a half-closed stream ended with an error) logs at DEBUG
and returns: no ERROR log, and no login loop after an auth error code once the
route has stopped.
No option changes. Not changed: events queued on the consumer worker pool can
be dropped when a stop exceeds the executor shutdown timeout (generic
worker-pool behaviour); the in-memory replay id moves past exchanges still in
flight (at-most-once design); LATEST resubscribe before the first response and
the opt-in {{fallbackToLatestReplayId}} can skip events.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)