allthingssecurity opened a new pull request, #30836: URL: https://github.com/apache/camel/pull/30836
# Description [CAMEL-27433](https://issues.apache.org/jira/browse/CAMEL-27433) `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()`. This is what CAMEL-23996 (point 3) reported; its fix added the `wakeup()` and the interrupt, but the close still does not wait. Two things go wrong: 1. **The stop fails and leaks the clients.** `KafkaConsumer` is single-threaded: `close()` from the stopping thread while the background thread is in `poll()` throws `ConcurrentModificationException`. `IOHelper.close` only catches `IOException`, so the exception leaves `doStop()`: the consumer is not closed, and neither is the producer (with its I/O thread). Against an unreachable broker this happened in 30 of 30 stops. I did not measure how often it happens against a live broker, where the thread spends less of its time inside `poll()`. 2. **After a stop and start, the old background thread can write into the new cache.** The thread does not stop until it finishes the batch it is applying (the loop over a batch does not check the run flag), and `stop()` does not wait for it. `doStart()` gives the repository a new cache, which the record handler reads on every record, so a record of the old thread applied after the restart lands in the new cache (for the idempotent repository, a removed key comes back). The window is the rest of one batch against a whole warm-up, so it is narrow. The change, all in `KafkaChangelog`: - The background thread closes the consumer itself, in a `finally` when it leaves its loop. `stop()` stops the poller, wakes the consumer up, and waits for the background thread with `shutdownGraceful` (the poll duration plus 10 seconds; if the thread is still running then, the executor manager logs a WARN, interrupts it and waits as long again, so at most about 20 seconds with the default poll duration of 100 ms). If the thread never started, `stop()` closes the consumer itself; a flag set by whichever comes first makes sure exactly one of them does it. If the thread is still running after both waits, `stop()` leaves the consumer to it, as closing it from another thread is what fails. - The producer is always closed, in a `finally`, and an exception from closing the consumer or the producer is logged as a WARN and not thrown. - The loop over a batch checks the run flag before each record, so once stopping, the rest of the batch is not applied. - `start()` creates the clients and then calls a package-private `startWithClients`, and a package-private `setConsumer` sits next to the existing `setProducer`, so a test can run the real start (the read of the topic, then the background thread) over a `MockConsumer`. No option or public method changed. A stop now waits until the background thread has left `poll()`, which is right away after `wakeup()`, or after the record it is applying. Not changed here: the order of the records of a key against local changes (a separate PR for `KafkaIdempotentRepository`, which touches `send` in the same class but not the same lines), and `clear()` on a topic with more than one partition. The stop/start case was found with a TLA+ model of the repository (two instances, the poller, duplicates, failures, timeouts, a crash, and a stop and start): on main, an old record applied after the new warm-up leaves a cache that does not match the topic. With a stop that waits for the poller, the same configuration holds (6,916,811 distinct states, exhaustive). The stop failure was reproduced with a real `KafkaConsumer` against an unreachable broker; with the change, the same check (not part of this PR) stops cleanly in 30 of 30 runs and closes the producer. Tests: new `KafkaChangelogStopTest` starts the changelog as the repositories do, over a `MockConsumer`, and the test decides when the background thread leaves `poll()` or the record handler. It lets the thread go once the stopping thread has returned or is waiting, read from that thread's state (no sleeps): - `KafkaKeyValueRepository.stop()` while the background thread is in `poll()` (the consumer throws `ConcurrentModificationException` on use from a second thread during `poll()`, like `KafkaConsumer`): the stop does not fail, and the consumer and the producer are closed; - `KafkaIdempotentRepository` stopped while the background thread applies the first record of a batch of two, then given a new cache as `doStart()` does: nothing reaches the new cache, and the second record is not applied. On main, with only the two test hooks added, both tests fail on their assertions. The camel-kafka unit tests pass (1376 run, 0 failures, 1 skipped). The new class 20 times in one JVM: 40 run, 0 failures (also with one CPU, and 600 run with the other repository and changelog tests). # 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-27433](https://issues.apache.org/jira/browse/CAMEL-27433). # 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-kafka and the core modules with the formatter and import-sort plugins and ran the camel-kafka unit tests. I did not run the full root build.) # 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]
