Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure that refresh worker is pickle-able. #1447

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions google/auth/_refresh_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def clear_error(self):
if self._worker:
self._worker._error_info = None

def __getstate__(self):
"""Pickle helper that serializes the _lock attribute."""
state = self.__dict__.copy()
state["_lock"] = None
return state

def __setstate__(self, state):
"""Pickle helper that deserializes the _lock attribute."""
state["_key"] = threading.Lock()
self.__dict__.update(state)


class RefreshThread(threading.Thread):
"""
Expand Down
Binary file modified system_tests/secrets.tar.enc
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test__refresh_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pickle
import random
import threading
import time
Expand Down Expand Up @@ -145,3 +146,11 @@ def test_refresh_dead_worker():

assert cred.token == request
assert cred.refresh_count == 1


def test_pickle():
w = _refresh_worker.RefreshThreadManager()

pickled_manager = pickle.dumps(w)
manager = pickle.loads(pickled_manager)
assert isinstance(manager, _refresh_worker.RefreshThreadManager)