Skip to content

Fix KafkaAppender reporting error after successful retry#4125

Open
SebTardif wants to merge 2 commits into
apache:2.xfrom
SebTardif:fix/kafka-appender-retry-logic
Open

Fix KafkaAppender reporting error after successful retry#4125
SebTardif wants to merge 2 commits into
apache:2.xfrom
SebTardif:fix/kafka-appender-retry-logic

Conversation

@SebTardif
Copy link
Copy Markdown

Fix KafkaAppender retry logic that always reports an error to the error handler even when a retry succeeds.

When retryCount is configured and the initial tryAppend() fails, the retry loop uses break to exit on success. However, break only exits the while loop; execution always reaches the error() call afterward. This causes spurious error notifications for transient Kafka failures that were successfully recovered by a retry.

This change replaces break with return so that a successful retry exits append() without reporting an error. Retry exceptions are now logged at DEBUG level for diagnostics instead of being silently discarded.

Also removes dead code in Builder.getRetryCount() where Integer.valueOf(int) was wrapped in a NumberFormatException catch that can never fire.

The bug was introduced in #315.

Checklist

  • Base your changes on 2.x branch if you are targeting Log4j 2; use main otherwise
  • ./mvnw verify succeeds (the build instructions)
  • Tests are provided

When retryCount is configured and the initial tryAppend() fails, the
retry loop uses break to exit on success. However, break only exits the
while loop and execution always reaches the error() call afterward. This
causes spurious error notifications for transient Kafka failures that
were successfully recovered by a retry.

Replace break with return so that a successful retry exits append()
without reporting an error. Retry exceptions are now logged at DEBUG
level for diagnostics instead of being silently discarded.

Also remove dead code in Builder.getRetryCount() where Integer.valueOf(int)
was wrapped in a NumberFormatException catch that can never fire.

The bug was introduced in apache#315.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Copy link
Copy Markdown
Contributor

@ramanathan1504 ramanathan1504 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall — great fix for the break/return retry behavior, and thanks for adding DEBUG logging plus cleaning up getRetryCount().

One small suggestion commented to keep the diff minimal: retain the existing while loop shape and just apply the early return + DEBUG logging inside it.

int currentRetryAttempt = 0;
while (currentRetryAttempt < this.retryCount) {
currentRetryAttempt++;
if (this.retryCount != null && this.retryCount > 0) {
Copy link
Copy Markdown
Contributor

@ramanathan1504 ramanathan1504 May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this.retryCount != null) {
                    int currentRetryAttempt = 0;
                    while (currentRetryAttempt < this.retryCount) {
                        currentRetryAttempt++;
                        try {
                            tryAppend(event);
                            return; // Fix: Exit early on success
                        } catch (final Exception retryException) {
                            LOGGER.debug(
                                    "Unable to write to Kafka in appender [{}], retry attempt [{}/{}].",
                                    getName(),
                                    currentRetryAttempt,
                                    this.retryCount,
                                    retryException);
                        }
                    }
                }

This still applies all three of your excellent fixes (early return, no swallowed exceptions, and preserving the stack trace in debug mode) but modifies fewer lines of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants