Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

fix: #1213 add OIDC retry and backoff to help prevent race conditions

Matthew Kilgore20073684cf92
diff --git a/backend/app/api/handlers/v1/helpers.go b/backend/app/api/handlers/v1/helpers.go
index 605b6b1d..aba003a4 100644
--- a/backend/app/api/handlers/v1/helpers.go
+++ b/backend/app/api/handlers/v1/helpers.go
@@ -67,6 +67,12 @@ func getScheme(r *http.Request, trustProxy bool) string {
// stripPathFromURL removes the path from a URL.
// ex. https://example.com/tools -> https://example.com
func stripPathFromURL(rawURL string) string {
+ // Validate that the URL has a scheme; if not, return empty string
+ if !strings.Contains(rawURL, "://") {
+ log.Warn().Str("url", rawURL).Msg("URL missing scheme")
+ return ""
+ }
+
parsedURL, err := url.Parse(rawURL)
if err != nil {
log.Err(err).Msg("failed to parse URL")
diff --git a/backend/app/api/providers/oidc.go b/backend/app/api/providers/oidc.go
index f2e399e8..ae64cbbc 100644
--- a/backend/app/api/providers/oidc.go
+++ b/backend/app/api/providers/oidc.go
@@ -55,12 +55,40 @@ func NewOIDCProvider(service *services.UserService, config *config.OIDCConf, opt
return nil, fmt.Errorf("OIDC issuer URL is required when OIDC is enabled (set HBOX_OIDC_ISSUER_URL)")
}
- ctx, cancel := context.WithTimeout(context.Background(), config.RequestTimeout)
- defer cancel()
+ // Attempt to create OIDC provider with retry logic for Docker Compose environments
+ // Retries up to 4 times with exponential backoff delays
+ var provider *oidc.Provider
+ var err error
+ maxRetries := 4
+ delays := []time.Duration{0, 500 * time.Millisecond, 2 * time.Second, 5 * time.Second}
+
+ for attempt := 1; attempt <= maxRetries; attempt++ {
+ ctx, cancel := context.WithTimeout(context.Background(), config.RequestTimeout)
+ provider, err = oidc.NewProvider(ctx, config.IssuerURL)
+ cancel()
+
+ if err == nil {
+ // Success on this attempt
+ if attempt > 1 {
+ log.Info().Int("attempt", attempt).Msg("OIDC provider initialized successfully after retry")
+ }
+ break
+ }
+
+ // Log the error and prepare for retry if we have more attempts
+ if attempt < maxRetries {
+ delay := delays[attempt-1]
+ log.Warn().
+ Err(err).
+ Int("attempt", attempt).
+ Dur("retrying_in", delay).
+ Msg("failed to create OIDC provider, retrying...")
+ time.Sleep(delay)
+ }
+ }
- provider, err := oidc.NewProvider(ctx, config.IssuerURL)
if err != nil {
- return nil, fmt.Errorf("failed to create OIDC provider from issuer URL: %w", err)
+ return nil, fmt.Errorf("failed to create OIDC provider from issuer URL after %d attempts: %w", maxRetries, err)
}
// Create ID token verifier