shashank created CAMEL-27433:
--------------------------------
Summary: camel-kafka - KafkaChangelog.stop() closes the consumer
while the background thread is in poll(), so the stop fails and leaks the
producer
Key: CAMEL-27433
URL: https://issues.apache.org/jira/browse/CAMEL-27433
Project: Camel
Issue Type: Bug
Components: camel-kafka
Reporter: shashank
Assignee: shashank
h3. What happens
{{KafkaIdempotentRepository}} and {{KafkaKeyValueRepository}} share
{{KafkaChangelog}}, which keeps a background thread polling the changelog
topic. {{KafkaChangelog.stop()}} calls {{consumer.wakeup()}}, interrupts the
thread, and then closes the consumer at once from the stopping thread, without
waiting for the background thread to leave {{poll()}}. CAMEL-23996 (point 3)
reported this; its fix added the {{wakeup()}} and the interrupt, but the close
still does not wait.
*1. The stop fails and leaks the clients.* {{KafkaConsumer}} is
single-threaded, so {{close()}} from the stopping thread while the background
thread is in {{poll()}} throws {{ConcurrentModificationException}}.
{{IOHelper.close}} only catches {{IOException}}, so the exception leaves
{{doStop()}}: neither the consumer nor the producer (with its I/O thread) is
closed.
*2. After a stop and start, the old background thread can write into the new
cache.* The loop over a batch does not check the run flag, {{stop()}} does not
wait for the thread, and {{doStart()}} gives the repository a new cache that
the record handler reads on every record. A record of the old thread applied
after the restart lands in the new cache. The window is narrow (the rest of one
batch).
h3. How to reproduce
A real {{KafkaConsumer}} pointed at an unreachable broker, assigned to one
partition, polled by the background loop; {{stop()}} throws
{{ConcurrentModificationException}} in 30 of 30 runs and the producer is not
closed. For the restart case: hold the background thread while it applies the
first record of a batch of two, stop the repository and give it a new cache,
then let the thread go on: both records land in the new cache.
h3. Fix
The background thread closes the consumer itself when it leaves its loop, and
{{stop()}} waits for it (the poll duration plus 10 seconds; if the thread is
still running then, the executor manager interrupts it and waits as long
again); if the thread never started, {{stop()}} closes the consumer. The
producer is always closed, and an exception from closing is logged as a WARN,
not thrown. The loop over a batch stops applying records once stopping. No
option or public method changes.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)