diff --git a/google/auth/_refresh_worker.py b/google/auth/_refresh_worker.py index cb115b939..9bb0ccc2c 100644 --- a/google/auth/_refresh_worker.py +++ b/google/auth/_refresh_worker.py @@ -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): """ diff --git a/system_tests/secrets.tar.enc b/system_tests/secrets.tar.enc index c5b080e8b..871eb5cbb 100644 Binary files a/system_tests/secrets.tar.enc and b/system_tests/secrets.tar.enc differ diff --git a/tests/test__refresh_worker.py b/tests/test__refresh_worker.py index 1fbbf1625..f842b02ca 100644 --- a/tests/test__refresh_worker.py +++ b/tests/test__refresh_worker.py @@ -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 @@ -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)