Skip to content

Commit be2d9c6

Browse files
yinghsienwucopybara-github
authored andcommitted
fix: Set event loop before asyncio.Lock() to ensure threading safety
PiperOrigin-RevId: 740168890
1 parent 38f5beb commit be2d9c6

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

google/genai/_api_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ def __init__(
332332
# Initialize the lock. This lock will be used to protect access to the
333333
# credentials. This is crucial for thread safety when multiple coroutines
334334
# might be accessing the credentials at the same time.
335+
try:
336+
asyncio.get_event_loop()
337+
except RuntimeError:
338+
asyncio.set_event_loop(asyncio.new_event_loop())
335339
self._auth_lock = asyncio.Lock()
336340

337341
# Handle when to use Vertex AI in express mode (api key).

0 commit comments

Comments
 (0)