GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(auth): restore the refresh worker when unpickling oauth2 credentials by farisbasha · Pull Request #19120 · googleapis/google-cloud-python

Conversation

@farisbasha

@farisbasha farisbasha commented Nov 18, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
@farisbasha
fix(auth): restore the refresh worker when unpickling oauth2 credentials
d2d0e3d
`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[bot]
gemini-code-assist Bot reviewed Nov 18, 2026
View reviewed changes

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/google-auth/tests/oauth2/test_credentials.py
# 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)

@gemini-code-assist gemini-code-assist Bot Nov 18, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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().

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

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

Reviewers

1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@farisbasha
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.