Repository navigation
fix: reduce out-of-range partitions modulo the partition count in getSendBrokers - #432
Merged
ShogunPanda merged 1 commit intoOct 1, 2026
Conversation
…SendBrokers getSendBrokers (and getSendConnections through it) computed the target partition with a bitwise AND against the partition count, while send() and the transaction path reduce it with a modulo. For a topic with 3 partitions, `partition: 3` gave `3 & 3 = 3`, an index that does not exist, so reading `.leader` threw a TypeError inside the metadata callback; `partition: 4` gave `4 & 3 = 0` although send() routes it to `4 % 3 = 1`, so the broker was reported under the wrong key. The AND form only agrees with the modulo when the count is a power of two. Use the same modulo reduction as send(). Signed-off-by: Fadi Romdhan <fadiromdhan3@gmail.com>
ShogunPanda
approved these changes
Oct 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Producer.getSendBrokers()(andgetSendConnections(), which builds on it) computed the target partition with a bitwise AND against the topic's partition count:while
send()(producer.ts) and the transaction path (transaction.ts) reduce it with a modulo:The two only agree when the partition count is a power of two and the raw partition is below it. For a 3-partition topic:
send()routes togetSendBrokers()before3 & 3 = 3→partitions[3]is undefined →TypeError: Cannot read properties of undefined (reading 'leader')inside the metadata callback4 & 3 = 0→ broker reported undertopic:0This PR uses the same modulo reduction as
send().Test
getSendBrokers should wrap out-of-range partitions like send doesintest/clients/producer/producer.test.ts: a 3-partition topic with messages on partitions 3, 4 and 2 must yield the keystopic:0,topic:1,topic:2. Onmainit fails with the TypeError above; with this change the fivegetSendBrokerstests pass against a Kafka 3.9.1 broker.pnpm lintis clean.