Skip to content

fix(auth): restore the refresh worker when unpickling oauth2 credentials - #19120

Open
farisbasha wants to merge 1 commit into
googleapis:mainfrom
farisbasha:fix-auth-unpickle-refresh-worker
Open

farisbasha wants to merge 1 commit into
googleapis:mainfrom
farisbasha:fix-auth-unpickle-refresh-worker

Conversation

@farisbasha

Copy link
Copy Markdown

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #19103 🦕

What was wrong

google.oauth2.credentials.Credentials.__getstate__ drops _refresh_worker (it holds a lock and a thread), and __setstate__ then set it to None. Non-blocking refresh uses it without a check (google/auth/credentials.py, _non_blocking_refresh): self._refresh_worker.start_refresh(...) for a stale token and self._refresh_worker.clear_error() after a blocking refresh. So an unpickled credential with with_non_blocking_refresh() enabled failed with AttributeError: 'NoneType' object has no attribute ..., as shown in the issue.

The base class CredentialsWithRegionalAccessBoundary.__setstate__ already creates a new RefreshThreadManager() when the worker is missing; only the oauth2 override set None.

Change

  • __setstate__ now creates a new RefreshThreadManager().
  • The two pickle tests that asserted the worker is None now assert it is a RefreshThreadManager.
  • New test test_pickle_and_unpickle_non_blocking_refresh: unpickles a credential with non-blocking refresh enabled and runs _non_blocking_refresh with refresh mocked. Before this change it raises AttributeError on clear_error().

I did not run nox locally; I'm relying on CI for the unit and lint sessions.

`google.oauth2.credentials.Credentials.__setstate__` set
`_refresh_worker` to None. Non-blocking refresh calls `start_refresh()`
and `clear_error()` on it, so an unpickled credential with non-blocking
refresh enabled failed with AttributeError. Create a new
`RefreshThreadManager` instead, as the base class `__setstate__` does.

Fixes googleapis#19103
@farisbasha
farisbasha requested review from a team as code owners November 18, 2026 05:25

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request ensures that non-blocking refresh continues to work after credentials are unpickled by initializing _refresh_worker with a new RefreshThreadManager instance instead of setting it to None. The tests are updated to reflect this change, and a new test is added to verify non-blocking refresh after deserialization. A review comment correctly identifies a pre-existing bug in the test assertions where list.sort() is used, causing the assertion to compare None == None and silently pass; it is recommended to use sorted() instead.

# The worker is not pickled; a new one is created.
if attr == "_refresh_worker":
assert getattr(unpickled, attr) is None
assert isinstance(getattr(unpickled, attr), RefreshThreadManager)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While reviewing this test, I noticed a pre-existing bug on line 980: assert list(creds.__dict__).sort() == list(unpickled.__dict__).sort(). In Python, list.sort() sorts the list in-place and returns None. Consequently, this assertion compares None == None, which always evaluates to True and silently passes without actually verifying that the dictionary keys match. To fix this, please use sorted() or compare the keys as sets, for example: assert sorted(creds.__dict__) == sorted(unpickled.__dict__) or assert creds.__dict__.keys() == unpickled.__dict__.keys().

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant