From 6dec84629968aacd723ab05372d533b96a26b6c0 Mon Sep 17 00:00:00 2001 From: Ben Browning Date: Thu, 2 Apr 2026 03:18:12 +0000 Subject: [PATCH] Fix GCP token refresh "context canceled" error The context passed to google.CredentialsFromJSON was created with a 30-second timeout and immediately canceled via defer when init() returned. The oauth2 library stores this context internally and reuses it for all subsequent token refresh HTTP calls, so every refresh attempt failed with "context canceled". Use context.Background() instead, which remains valid for the lifetime of the proxy. Co-Authored-By: Claude Opus 4.6 --- internal/credentials/gcloud.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/credentials/gcloud.go b/internal/credentials/gcloud.go index 8599493..e07a98a 100644 --- a/internal/credentials/gcloud.go +++ b/internal/credentials/gcloud.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "sync" - "time" "golang.org/x/oauth2/google" ) @@ -57,10 +56,10 @@ func (g *GCloudInjector) init() error { } } - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - - creds, err := google.CredentialsFromJSON(ctx, data, g.scopes...) + // Use context.Background() — this context is stored by the oauth2 + // library and reused for all token refresh HTTP calls. It must NOT + // be canceled or have a short timeout. + creds, err := google.CredentialsFromJSON(context.Background(), data, g.scopes...) if err != nil { g.initErr = fmt.Errorf("parse ADC credentials: %w", err) return