Skip to content

Recreate producer after InvalidPidMappingException - #4919

Merged
sobychacko merged 1 commit into
spring-projects:mainfrom
wn1331:GH-4907
Nov 12, 2026
Merged

sobychacko merged 1 commit into
spring-projects:mainfrom
wn1331:GH-4907

Conversation

@wn1331

@wn1331 wn1331 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Kafka clients treat InvalidPidMappingException as fatal to the current producer, but DefaultKafkaProducerFactory kept the failed non-transactional producer cached. Subsequent sends therefore reused the same defunct producer.

This change:

  • removes and closes a producer when its send callback receives InvalidPidMappingException;
  • preserves the existing OutOfOrderSequenceException behavior;
  • verifies that the next factory request creates a fresh producer.

The factory does not replay failed records, so applications retain responsibility for handling the failed send and its delivery semantics.

Validation:

  • ./gradlew :spring-kafka:test --tests org.springframework.kafka.core.DefaultKafkaProducerFactoryTests
  • ./gradlew :spring-kafka:checkstyleMain :spring-kafka:checkstyleTest
  • ./gradlew :spring-kafka:check

Fixes #4907

Fixes: spring-projects#4907

Kafka clients treat InvalidPidMappingException as fatal to the current
producer. DefaultKafkaProducerFactory kept that producer cached, so every
later send reused the defunct instance.

Remove and close the producer when the send callback receives this exception,
as already done for OutOfOrderSequenceException, and verify that the next
request creates a fresh producer.

Signed-off-by: Jonghun Ju <85150438+wn1331@users.noreply.github.com>
@ferblaca

ferblaca commented Nov 11, 2026 •

Copy link
Copy Markdown

Thanks a lot for taking this issue and for providing such a focused fix! This looks like a good fit for the problem described in #4907: the failed CloseSafeProducer is evicted when the asynchronous send callback receives InvalidPidMappingException, so the next factory request can create a fresh producer. The regression test is also clear and nicely reuses the existing UnknownProducerIdException coverage.

I had one question about transactional producers. Kafka Clients 4.x currently transitions InvalidPidMappingException to FATAL_ERROR before checking isTransactional(), so the exception can also leave a transactional producer unusable. In that case, removing the failed producer from the transactional cache may be the right behavior as well, but it would be great to make the semantics explicit with an additional test.

Would it be possible to add a transactional DefaultKafkaProducerFactory regression test covering that:

  • the failed producer is removed from the transactional cache;
  • its transactional-id suffix is released;
  • the next producer is initialized through initTransactions();
  • the failed transaction is not reused by the new producer?

This is not intended as a blocker—the current change looks correct for the non-transactional scenario and the existing behavior already handles other fatal producer errors in the same way. The extra test would just give us confidence that the transactional cache and transaction lifecycle are also covered.

Thanks again!

@ferblaca

ferblaca commented Nov 11, 2026 •

Copy link
Copy Markdown

Just to clarify: I am not a Spring Kafka maintainer, so please consider this as feedback from an integration user based on the production scenario behind #4907. The Spring Kafka maintainers have the final say on the design, scope, and transactional semantics of this change. Thanks again for taking the time to investigate and contribute the fix.

@sobychacko
sobychacko merged commit 934fdf1 into spring-projects:main Nov 12, 2026
3 checks passed
spring-builds pushed a commit that referenced this pull request Nov 12, 2026
Fixes: #4907

Kafka clients treat InvalidPidMappingException as fatal to the current
producer. DefaultKafkaProducerFactory kept that producer cached, so every
later send reused the defunct instance.

Remove and close the producer when the send callback receives this exception,
as already done for OutOfOrderSequenceException, and verify that the next
request creates a fresh producer.

Signed-off-by: Jonghun Ju <85150438+wn1331@users.noreply.github.com>
(cherry picked from commit 934fdf1)
spring-builds pushed a commit that referenced this pull request Nov 12, 2026
Fixes: #4907

Kafka clients treat InvalidPidMappingException as fatal to the current
producer. DefaultKafkaProducerFactory kept that producer cached, so every
later send reused the defunct instance.

Remove and close the producer when the send callback receives this exception,
as already done for OutOfOrderSequenceException, and verify that the next
request creates a fresh producer.

Signed-off-by: Jonghun Ju <85150438+wn1331@users.noreply.github.com>
(cherry picked from commit 934fdf1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recreate idempotent non-transactional producer after InvalidPidMappingException

3 participants