From caf6a47aecd5255be3206786a932a9d9a16468bf Mon Sep 17 00:00:00 2001 From: Google Colaboratory Team Date: Thu, 4 Jan 2024 13:09:34 -0800 Subject: [PATCH] No public description PiperOrigin-RevId: 595784198 --- google/colab/userdata.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/google/colab/userdata.py b/google/colab/userdata.py index 48d9861f..76e7320e 100644 --- a/google/colab/userdata.py +++ b/google/colab/userdata.py @@ -1,5 +1,7 @@ """API to access user secrets.""" +import threading + from google.colab import _message from google.colab import errors @@ -17,10 +19,14 @@ class SecretNotFoundError(errors.Error): def __init__(self, key): super().__init__(f'Secret {key} does not exist.') +_userdata_lock = threading.Lock() + def get(key): """Fetches the value for specified secret keys. + This is safe to use from multiple threads. + Args: key: Identifier of the secret to fetch. @@ -32,9 +38,12 @@ def get(key): secret. SecretNotFoundError: If the requested secret is not found. """ - resp = _message.blocking_request( - 'GetSecret', request={'key': key}, timeout_sec=None - ) + # blocking_request is not thread-safe, use a global lock to keep the function + # thread-safe. + with _userdata_lock: + resp = _message.blocking_request( + 'GetSecret', request={'key': key}, timeout_sec=None + ) if not resp.get('exists', False): raise SecretNotFoundError(key) if not resp.get('access', False):