Skip to content

fix(client): add nilguards to updateBroker#3393

Merged
dnwe merged 1 commit intomainfrom
dnwe/nilguard
Nov 27, 2025
Merged

fix(client): add nilguards to updateBroker#3393
dnwe merged 1 commit intomainfrom
dnwe/nilguard

Conversation

@dnwe
Copy link
Copy Markdown
Collaborator

@dnwe dnwe commented Nov 27, 2025

This should be a rare edge case as the updateBroker func is only called from updateMetadata which does already do an up-front client.Close() check under read-lock before then acquiring the write lock. However, there's potentially a small window of opportunity that if client.Close() was called whilst metadata refresh was in-flight and for whatever reason the updateMetadata goroutine gets pre-empted in-between the readlock release and the write lock acquire then the client could have been closed and so client.brokers will be nil.

I wouldn't have expected this to ever happen, but it was reported by a user in an older Sarama version in #3391 and there's no harm in adding the nilguard just in case.

This _should_ be a rare edge case as the updateBroker func is only
called from updateMetadata which does already do an up-front
`client.Close()` check under read-lock before then acquiring the write
lock. However, there's potentially a small window of opportunity that if
client.Close() was called whilst metadata refresh was in-flight and for
whatever reason the updateMetadata goroutine gets pre-empted in-between
the readlock release and the write lock acquire then the client could
have been closed and so `client.brokers` will be nil.

I wouldn't have expected this to ever happen, but it was reported by a user in an older
Sarama version in #3391 and there's no harm in adding the nilguard just
in case.

Signed-off-by: Dominic Evans <[email protected]>
@dnwe dnwe added the fix label Nov 27, 2025
Copy link
Copy Markdown
Collaborator

@hindessm hindessm left a comment

Choose a reason for hiding this comment

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

LGTM

@dnwe dnwe merged commit da8cb80 into main Nov 27, 2025
17 checks passed
@dnwe dnwe deleted the dnwe/nilguard branch November 27, 2025 16:12
DCjanus pushed a commit to DCjanus/sarama that referenced this pull request Dec 8, 2025
This _should_ be a rare edge case as the updateBroker func is only
called from updateMetadata which does already do an up-front
`client.Close()` check under read-lock before then acquiring the write
lock. However, there's potentially a small window of opportunity that if
client.Close() was called whilst metadata refresh was in-flight and for
whatever reason the updateMetadata goroutine gets pre-empted in-between
the readlock release and the write lock acquire then the client could
have been closed and so `client.brokers` will be nil.

I wouldn't have expected this to ever happen, but it was reported by a
user in an older Sarama version in IBM#3391 and there's no harm in adding
the nilguard just in case.

Signed-off-by: Dominic Evans <[email protected]>
Signed-off-by: DCjanus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants