antonio-tomac opened a new pull request #10933: URL: https://github.com/apache/kafka/pull/10933
Jira issue (detailed description): https://issues.apache.org/jira/browse/KAFKA-12999 ## Summary After upgrading clients to `2.8.0`, reading `ConsumerRecord`'s header keys started resulting in occasional `java.lang.NullPointerException` in case of concurrent access from multiple(2) threads. ### NPE location NPE happens here RecordHeader.java:45: ```java public String key() { if (key == null) { key = Utils.utf8(keyBuffer, keyBuffer.remaining()); // NPE here keyBuffer = null; } return key; } ``` ### What introduced issue Cause of issue is introduced by changes of apache#9223 ### Consequences Current implementation renders RecordHeader not thread-safe for read-only access. ### Reproducibility Since issue s concurrent race condition, reproducibility is non-deterministic but easy to reproduce with enough re-attempts This PR has test which almost always reproduces issue (wiithout `synchronized`) ### Fix strategy This PR avoids race-condition by having `synchronized` on `key()` and `value()` methods ### Benchmark Comparison between with and without `synchronized` on `RecordHeader.key()` method ``` Benchmark Mode Cnt Score Error Units RecordHeaderBenchmark.key avgt 15 31.308 ± 7.862 ns/op RecordHeaderBenchmark.synchronizedKey avgt 15 31.853 ± 7.096 ns/op ```` ### Committer Checklist (excluded from commit message) - [ ] Verify design and implementation - [ ] Verify test coverage and CI build status - [ ] Verify documentation (including upgrade notes) -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org