Skip to content

Conversation

@1911860538
Copy link
Contributor

This PR fixes potential issues in the Eureka client's server picking and retry logic.
e.maxRetry is not necessarily equal to len(e.urls),
because users can customize e.maxRetry using the WithMaxRetry function.
When users set e.maxRetry=0, the for-loop in the Client.do method will not execute.
When e.maxRetry is greater than len(e.urls), array index out of bounds may occur.
For example, if a user sets e.maxRetry=10 and len(e.urls)=2,
when pickServer is executed with currentTimes=2,
currentTimes%e.maxRetry=2, resulting in an array index out of bounds.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Dec 17, 2025
@1911860538
Copy link
Contributor Author

Reopen #3771
On the main branch, the following code can reproduce the array out-of-bounds issue.

kratos/contrib/registry/eureka/client_test.go

package eureka

import (
	"context"
	"testing"
)

// This test demonstrates an out-of-bounds panic in pickServer
// when maxRetry > len(urls).
func TestPickServer_OutOfBoundsPanic(t *testing.T) {
	// len(urls) = 2
	e := NewClient(
		[]string{
			"http://127.0.0.1:8080",
			"http://127.0.0.1:8081",
		},
		WithMaxRetry(3), // > len(urls)
	)

	// This call will eventually invoke pickServer with currentTimes = 2,
	// which leads to e.urls[2%3] == e.urls[2] and panics:
	// runtime error: index out of range [2] with length 2
	_ = e.Out(context.Background(), "app", "instance")
}
截屏2025-12-17 19 53 41

@1911860538 1911860538 closed this Dec 18, 2025
@1911860538 1911860538 reopened this Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant