Skip to content

Commit

Permalink
fix: Ensure that refresh worker is pickle-able. (#1447)
Browse files Browse the repository at this point in the history
* fix: Ensure that refresh worker is pickle-able.
  • Loading branch information
clundin25 authored Jan 3, 2024
1 parent a99886c commit 421c184
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
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)

0 comments on commit 421c184

Please sign in to comment.