Skip to content

Fix race condition in _GENAI_CLIENT initialization using async lock#102

Open
kirandharmar867-maker wants to merge 2 commits intoINCF:mainfrom
kirandharmar867-maker:_GENAI_CLIENT
Open

Fix race condition in _GENAI_CLIENT initialization using async lock#102
kirandharmar867-maker wants to merge 2 commits intoINCF:mainfrom
kirandharmar867-maker:_GENAI_CLIENT

Conversation

@kirandharmar867-maker
Copy link
Copy Markdown

Issue: _GENAI_CLIENT singleton not thread-safe on cold start

Root Cause:

The GenAI client was being initialized lazily without any synchronization.
When multiple requests hit the server at the same time (especially during cold start), they could all see _GENAI_CLIENT as None and create multiple instances.

Changes:
Added an asyncio.Lock to make initialization thread-safe.
Used double-check locking to ensure the client is created only once.
Converted _get_genai_client() to async.
Updated all usages to await _get_genai_client().

Impact:
Prevents duplicate client creation
Fixes race condition during concurrent requests
Makes initialization safe and stable
No change in existing functionality

Steps to Test:
Run the backend server
Send multiple requests at the same time
Verify only one client is initialized and everything works normally

Files Modified:
backend/agents.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant