Repository navigation
fix(auth): restore the refresh worker when unpickling oauth2 credentials - #19120
farisbasha wants to merge 1 commit into
Conversation
`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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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().
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:
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 toNone. Non-blocking refresh uses it without a check (google/auth/credentials.py,_non_blocking_refresh):self._refresh_worker.start_refresh(...)for a stale token andself._refresh_worker.clear_error()after a blocking refresh. So an unpickled credential withwith_non_blocking_refresh()enabled failed withAttributeError: 'NoneType' object has no attribute ..., as shown in the issue.The base class
CredentialsWithRegionalAccessBoundary.__setstate__already creates a newRefreshThreadManager()when the worker is missing; only the oauth2 override setNone.Change
__setstate__now creates a newRefreshThreadManager().Nonenow assert it is aRefreshThreadManager.test_pickle_and_unpickle_non_blocking_refresh: unpickles a credential with non-blocking refresh enabled and runs_non_blocking_refreshwithrefreshmocked. Before this change it raisesAttributeErroronclear_error().I did not run nox locally; I'm relying on CI for the unit and lint sessions.