net/valkey: fix nil pointer panic in shardForKey - #4173
Draft
madumalt wants to merge 1 commit into
Draft
Conversation
shardForKey dereferences the atomic pointer without a nil check:
return *vr.shards[xxhash.Sum64String(key)%ringSize].Load()
When shard slots are not yet initialized (e.g. ring created with zero
addresses before AddrUpdater populates them), Load() returns nil and
the dereference panics. This crashes every request hitting a
Valkey-backed cluster rate limit filter:
panic caused by: runtime error: invalid memory address or nil pointer dereference
github.com/zalando/skipper/net.(*valkeyRing).shardForKey(0x363fc1b64000, {0x363fc15dcc00?, 0x0?})
github.com/zalando/skipper/net/valkey.go:249 +0x64
Change shardForKey to return (valkey.Client, error) and check for nil
before dereferencing. Propagate the error through all valkeyRing
methods and ValkeyRingClient wrappers so callers receive a proper error
instead of a panic.
An alternative approach would be to prevent nil from ever being stored
by ensuring newValkeyRing does not return a usable ring until
updateShards has run with at least one address, and guarding SetAddr
against partial initialization races. This would be a stronger
invariant but a larger change; the nil-check in shardForKey is the
minimal safety net that prevents the panic regardless of cause.
Signed-off-by: Thilina Madumal <thilina.madumal@zalando.de>
Contributor
Author
Logs of the Panic |
szuecs
reviewed
Aug 7, 2026
| // shardForKey does the lookup for valkey most operations to find the valkey ring shard | ||
| func (vr *valkeyRing) shardForKey(key string) valkey.Client { | ||
| return *vr.shards[xxhash.Sum64String(key)%ringSize].Load() | ||
| func (vr *valkeyRing) shardForKey(key string) (valkey.Client, error) { |
Member
There was a problem hiding this comment.
we should not have a change here. We should fix the initial creation.
Contributor
Author
There was a problem hiding this comment.
Yeah that's what I proposed in the alternative approach. But atm I don't have all information how can I do it. Any leads?
Member
There was a problem hiding this comment.
I think in skipper.go we create the valkey client. We could expose Ready() bool in valkey client and we do something like for !valkey.Ready() { time.Sleep(time.Second) } in skipper.go before we start the main listener that would make skipper "ready" for NLB and kubernetes.
Member
There was a problem hiding this comment.
Ready() should check that the sharding client array has no nil entry or something like that.
madumalt
marked this pull request as draft
August 7, 2026 09:17
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.
shardForKey dereferences the atomic pointer without a nil check:
When shard slots are not yet initialized (e.g. ring created with zero addresses before AddrUpdater populates them), Load() returns nil and the dereference panics. This crashes every request hitting a Valkey-backed cluster rate limit filter:
Change shardForKey to return (valkey.Client, error) and check for nil before dereferencing. Propagate the error through all valkeyRing methods and ValkeyRingClient wrappers so callers receive a proper error instead of a panic.
Alternative Approach
An alternative approach would be to prevent nil from ever being stored by ensuring newValkeyRing does not return a usable ring until updateShards has run with at least one address, and guarding SetAddr against partial initialization races. This would be a stronger invariant but a larger change; the nil-check in shardForKey is the minimal safety net that prevents the panic regardless of cause.