Repository navigation
fix(auth): invalidate cached ADC credentials and clients on file change or auth error - #248
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces robust credential and client caching mechanisms, including cache invalidation when Application Default Credentials (ADC) or custom credential files are modified on disk. It also adds automatic cache eviction upon encountering authentication errors during API calls, backed by comprehensive unit tests. The feedback recommends using os.path.expanduser in file_stat_stamp and adc_file_stamp to correctly resolve and track credential file paths that contain user directory shortcuts (like ~).
tim-u
approved these changes
Dec 4, 2026
…ge or auth error PR #211 introduced process-wide caching for Application Default Credentials (_CREDENTIALS_CACHE in client/proxy.py) and Dataproc SessionControllerClient (_CLIENT_CACHE in session.py) keyed only on client options and environment variables. In long-lived notebook kernels, re-authenticating on disk with `gcloud auth application-default login` after RAPT or refresh-token expiry left both caches pinned to the stale pre-reauth credentials until kernel restart or a manual `clear_dataproc_caches()` call. Two complementary fixes: 1. Proactive file-stat invalidation: track `(abs_path, (st_mtime_ns, st_ctime_ns, st_size, st_ino, st_dev))` for the active ADC file (`GOOGLE_APPLICATION_CREDENTIALS` or the gcloud ADC path, resolved purely in Python with no subprocesses) and include `GOOGLE_CLOUD_QUOTA_PROJECT` in cache keys. Unify `SessionControllerClient` and `SessionTemplateControllerClient` through `_create_dataproc_client()`, validating `_CLIENT_CACHE` entries by credential object identity (`entry_creds is creds`) for default ADC or by file/ADC stat fingerprint for custom `ClientOptions` (`credentials_file`, `quota_project_id`, `scopes`). Replacing or deleting the credentials file on disk now invalidates both caches automatically on the next call. 2. Reactive auth-error eviction: evict both `_CREDENTIALS_CACHE` and `_CLIENT_CACHE` when a Dataproc control-plane RPC (including best-effort `GetSessionTemplate`) fails with `GoogleAuthError`, `Unauthorized` (covering both gRPC `Unauthenticated` and REST HTTP 401), or a gRPC auth metadata plugin `ServiceUnavailable` ("Getting metadata from plugin failed" / "Reauthentication is needed"), and evict the failed credentials entry when a token refresh or WebSocket TCP bridge handshake fails with HTTP 401. Also avoid pinning cached `SessionControllerClient` or `SessionTemplateControllerClient` instances onto `Builder` (while preserving explicit test injection and per-builder memoization for uncacheable `client_cert_source` options).
dborowitz
force-pushed
the
dborowitz-adc-cache-invalidation
branch
from
December 4, 2026 21:03
a2bf81b to
a75ee39
Compare
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.
Summary
Automatically invalidates cached Application Default Credentials (
_CREDENTIALS_CACHE) and Dataproc control-plane clients (_CLIENT_CACHE) when the underlying credentials file orGOOGLE_CLOUD_QUOTA_PROJECTchanges, or when an auth/credential refresh error occurs—fixing a regression from #211 where re-runninggcloud auth application-default loginin a terminal did not take effect in an already-running notebook kernel without restarting the kernel.Problem
#211 added process-wide caching for
google.auth.default()(_CREDENTIALS_CACHEinclient/proxy.py) andSessionControllerClient(_CLIENT_CACHEinsession.py) keyed on client options and environment variables. In long-lived interactive sessions (such as JupyterLab):SessionControllerClientandSessionTemplateControllerClientRPCs fail inside gRPC'sAuthMetadataPlugin(503 ServiceUnavailable: Getting metadata from plugin failed with error: Reauthentication is needed...) or REST'sAuthorizedSession(RefreshError/401 Unauthorized), bypassingget_refreshed_credentials().gcloud auth application-default loginupdates~/.config/gcloud/application_default_credentials.jsonon disk, but neither_CREDENTIALS_CACHEnor_CLIENT_CACHEnoticed the file change—and even whenget_refreshed_credentials()evicted_CREDENTIALS_CACHE,_CLIENT_CACHEandBuilder._session_template_controller_clientstill pinned the stale credentials/clients.Changes
client/proxy.py,session.py):file_stat_stamp()((st_mtime_ns, st_ctime_ns, st_size, st_ino, st_dev)) andadc_file_stamp()helpers (usingGOOGLE_APPLICATION_CREDENTIALSorgoogle.auth._cloud_sdk.get_application_default_credentials_path(), with zero subprocesses) and stored(adc_stamp, (creds, project))in_CREDENTIALS_CACHE, statting before read so concurrent rewrites are never tagged with a post-read stamp.GOOGLE_CLOUD_QUOTA_PROJECTin both_CREDENTIALS_CACHEand_CLIENT_CACHEcache keys.SessionControllerClientandSessionTemplateControllerClientcreation in_create_dataproc_client(), validating default-ADC entries in_CLIENT_CACHEby credential object identity (entry_creds is creds)—automatically staying in sync with_CREDENTIALS_CACHEreloads and clears—and validating customClientOptionsentries (credentials_fileviaabspath+ stat stamp,quota_project_id,scopes, falling back toadc_file_stamp()whencredentials_file == "").Builder.session_controller_clientandBuilder.session_template_controller_clientnot to pin cacheable clients onselfwhile preserving explicit test injection and per-builder memoization for uncacheableclient_cert_source/client_encrypted_cert_sourceoptions.session.py,_session_templates.py,client/proxy.py):_is_auth_error()/_maybe_evict_auth_caches()to clear_CREDENTIALS_CACHEand_CLIENT_CACHEwhen a Dataproc control-plane RPC fails withGoogleAuthError,Unauthorized(covering both gRPCUnauthenticatedand REST HTTP 401), or a gRPC auth-metadata-pluginServiceUnavailable(Getting metadata from plugin failed/Reauthentication is needed), without churning caches on transient non-auth 503s._maybe_evict_auth_caches()into allSessionControllerClientcontrol-plane error paths and intoBuilder._get_session_template()via anon_errorhook on_session_templates.fetch_session_template()._evict_credentials(creds)so refresh failures and WebSocket TCP bridge HTTP 401 errors inconnect_tcp_bridge()evict only cache entries referencing the failed credentials instance (without wiping concurrently rotated credentials).tests/unit/test_dataproc.py):CLOUDSDK_CONFIG,GOOGLE_APPLICATION_CREDENTIALS, relative/emptycredentials_file,quota_project_id,GOOGLE_CLOUD_QUOTA_PROJECT), cross-cache rebuild when_CREDENTIALS_CACHEis cleared or refresh fails,Builder.session_controller_clientandBuilder.session_template_controller_clientreauth tracking and injection/cert-source memoization,_is_auth_error()classification (including RESTUnauthorized), reactive eviction on gRPC/REST auth errors and_get_session_template()vs. non-auth 503s, and concurrent WebSocket 401 cache eviction.Verification
pyink --checkon modified Python files: passedpytest -q -p no:cacheprovider tests/unit/test_dataproc.py tests/unit/test_session_templates.py: 43 passed, 25 subtests passed