Skip to content

google-auth: non-blocking refresh fails after OAuth2 Credentials pickle roundtrip #19103

Open

Description

@Rajatendu1

Determine this is the right repository

  • I determined this is the correct repository in which to report this bug. This is a local google-auth credential-state problem, not an API service error; it reproduces without an HTTP request.

Summary of the issue

google.oauth2.credentials.Credentials.__setstate__() restores _refresh_worker to None, but non-blocking refresh assumes it is a RefreshThreadManager.

After a pickle roundtrip:

  • A stale token with non-blocking refresh enabled raises AttributeError: 'NoneType' object has no attribute 'start_refresh' before refreshing.
  • An expired token raises AttributeError: 'NoneType' object has no attribute 'clear_error' even after a successful blocking refresh. This second case was tested by mocking Credentials.refresh to set a fresh token, without network I/O.

This happens both when with_non_blocking_refresh() is called before pickling and when it is enabled only after unpickling. Fresh tokens and the default blocking-refresh mode are controls that pass.

Expected: pickled credentials should retain a usable refresh path when non-blocking refresh is enabled, without trying to serialize a running thread.

API client name and version

google-auth 2.62.2, current monorepo main c6af78de2507be088fe9e6e93b017d6375425cf1.

Reproduction steps: code

No real tokens, credentials, cloud account, or network calls are needed:

import pickle
from datetime import timedelta
from unittest import mock

from google.auth import _helpers
from google.oauth2.credentials import Credentials

cred = Credentials(
    token="synthetic-local-token",
    expiry=_helpers.utcnow() + timedelta(seconds=60),
)
cred.with_non_blocking_refresh()
restored = pickle.loads(pickle.dumps(cred))
request = mock.Mock(side_effect=AssertionError("Unexpected HTTP call"))

print("restored worker:", restored._refresh_worker)
try:
    restored.before_request(request, "GET", "https://example.invalid/", {})
finally:
    print("HTTP calls:", request.call_count)

Alternatively, move cred.with_non_blocking_refresh() to restored.with_non_blocking_refresh() after the roundtrip. The same exception occurs.

Reproduction steps: actual results

restored worker: None
HTTP calls: 0
...
  File "google/auth/credentials.py", line 211, in _non_blocking_refresh
    use_blocking_refresh_fallback = not self._refresh_worker.start_refresh(
AttributeError: 'NoneType' object has no attribute 'start_refresh'

Reproduction steps: expected results

No missing-worker exception. A stale credential should start the normal background refresh, and an invalid credential should be able to complete its blocking fallback and clear any worker error state.

OS & version + platform

Linux x86_64, local test environment.

Python environment

Python 3.10.12.

Python dependencies

google-auth==2.62.2 from the checkout; pytest==9.1.1, pytest-asyncio==1.5.0, cryptography==50.0.2, requests==2.34.2. Installed the package's testing extra plus mock and asyncmock for its native tests.

Additional context

  • The unmodified package's pytest tests tests_async baseline passes: 2,365 passed, 7 skipped, 90 warnings.
  • A separate nine-case local matrix has four failures and five passing controls. It covers stale/expired/fresh tokens, enabling non-blocking refresh before versus after the roundtrip, and the unchanged default blocking mode. Refresh is mocked, and the request mock is never called.
  • Existing pickle tests explicitly expect the restored worker to be None, but do not exercise subsequent non-blocking before_request() calls. It may therefore be preferable to lazily restore the manager rather than change that serialized-state contract.
  • I checked the earlier standalone-repo Adding optional content type argument to generate signed URI method #1447/Upgrading to oauth2client 2.0. #1479 pickle fixes and Pin back oauth2client from breaking major version #1478 discussion; they concern serializing the manager's lock, not this OAuth2 credential continuation path.
  • No fix implementation has been written or submitted. Following the issue-first guidance, would a focused fix that restores a usable manager for this path be welcome? Validation above is Linux/Python 3.10 only, not the full version/OS matrix or live cloud tests.

Investigation and reproduction tests developed with AI assistance.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions