shashank created CAMEL-25278:
--------------------------------

             Summary: camel-cometd - a stopped consumer stays subscribed to its 
channel (each message is processed twice after a route restart), and the 
extensions and server listeners are added again for every producer and consumer
                 Key: CAMEL-25278
                 URL: https://issues.apache.org/jira/browse/CAMEL-25278
             Project: Camel
          Issue Type: Bug
          Components: camel-cometd
            Reporter: shashank


The producers and consumers of a host and port share one CometD server 
({{CometdComponent.connect}} / {{disconnect}} with a reference count); the 
server is only stopped with the last of them.

h3. 1. A stopped consumer keeps listening to its channel

{{CometdConsumer.doStart}} creates a {{ConsumerService}} 
({{AbstractService.addService(channel, "push")}}, a listener on the channel) 
and {{doStop}} only calls {{endpoint.disconnect(this)}}: the listener stays on 
the channel. When the server outlives the consumer (a producer, or a consumer 
of another channel, on the same host and port) the old listener keeps calling 
the processor of the route. After a restart of the route there are two 
listeners, and every message is processed twice (three times after two 
restarts, ...).

h3. 2. Extensions and server listeners are added for every producer and consumer

{{CometdComponent.connect}} adds the configured {{extensions}} and 
{{serverListeners}} to the {{BayeuxServerImpl}} on every call, also when the 
server already exists. {{BayeuxServerImpl}} keeps them in a 
{{CopyOnWriteArrayList}}, so with a producer and a consumer on the same host 
and port each extension and listener is called twice per message or session 
(and once more after each restart). An extension that changes or counts 
messages (or does authentication work) runs several times.

h3. Reproduction

{{CometdConsumerRestartTest}} (a producer route and a consumer route on the 
same host and port, in process):
* stop and start the consumer route, send one message: main delivers it twice 
({{mock://test Received message count. Expected: <1> but was: <2>}});
* a {{SessionListener}} registered with {{addServerListener}} is called twice 
for one new session ({{expected: <1> but was: <2>}});
* control: a stopped consumer route does not receive (passes on main too: the 
stopped route refuses the exchange).

h3. Proposed fix

* {{CometdConsumer.doStop}}: remove the service from the channel and disconnect 
its local session.
* {{CometdComponent.connect}}: set the security policy and add the extensions 
and server listeners only when the server is created.
With the fix the camel-cometd tests pass (26).

Affected: 4.14.x, 4.18.x and main (same code).

Duplicate check (2026-10-02): JIRA component camel-cometd / text "cometd" (87 
issues, mostly camel-salesforce): none about consumer stop or duplicate 
extensions (CAMEL-23507 header filter, CAMEL-12267 transport properties). 
GitHub pull requests "cometd consumer", "cometd extension": none; open #22358 
(getOut cleanup) touches another line of {{CometdConsumer}}.

_Filed with Claude Code on behalf of allthingssecurity._




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to