Skip to content

make GenAI client initialization thread-safe (#82) - #110

Open
Arunpurohit1766 wants to merge 1 commit into
INCF:mainfrom
Arunpurohit1766:fix/threadsafe-genai-singleton
Open

Arunpurohit1766 wants to merge 1 commit into
INCF:mainfrom
Arunpurohit1766:fix/threadsafe-genai-singleton

Conversation

@Arunpurohit1766

Copy link
Copy Markdown

Summary of Changes

Resolves #82 by implementing thread-safe lazy initialization for the _GENAI_CLIENT singleton in backend/agents.py using double-checked locking, and adds a dedicated regression test suite.

Motivation & Root Cause

In backend/agents.py, _get_genai_client() previously used an unsynchronized lazy initialization pattern. Under concurrent cold starts (e.g., simultaneous incoming chat requests to FastAPI), multiple threads would evaluate _GENAI_CLIENT is None as true, leading to redundant client constructions, orphaned connection pools, and memory leaks.

Implementation Details

  • Double-Checked Locking: Introduced a module-level threading.Lock (_CLIENT_LOCK). Warm requests return immediately without acquiring the lock (zero performance penalty), while cold requests synchronize before initializing the client.
  • Initialization Failure Safety: _GENAI_CLIENT is assigned only after successful client construction. If construction fails (e.g. invalid credentials or transient network error), the singleton remains None and the lock is cleanly released, allowing subsequent requests to retry.
  • Resource Lifecycle in Reset: Added _reset_genai_client() for test isolation. It safely calls client.close() if exposed by the installed SDK to release active sockets before resetting the reference.
  • Preserved Existing Contracts: Preserves both Vertex AI and API-key modes, all environment variables (GCP_PROJECT_ID, GCP_REGION, GOOGLE_API_KEY), and default values.

Verification

Created tests/test_agents_client.py covering 6 regression scenarios:

  1. test_concurrent_cold_start_singleton: Synchronizes 20 worker threads via threading.Barrier hitting cold start simultaneously. Asserts constructor is called exactly once and all threads receive the exact same instance.
  2. test_warm_start_singleton: Asserts cached instance is returned without constructor re-invocation.
  3. test_vertex_ai_initialization: Asserts Vertex arguments and regions are preserved.
  4. test_api_key_initialization: Asserts API-key mode correctly passes credentials.
  5. test_initialization_failure_and_retry: Asserts state recovery after transient constructor errors.
  6. test_reset_genai_client: Asserts .close() is invoked and state resets cleanly.

All 6 tests pass in pytest:

tests/test_agents_client.py::test_concurrent_cold_start_singleton PASSED
tests/test_agents_client.py::test_warm_start_singleton PASSED
tests/test_agents_client.py::test_vertex_ai_initialization PASSED
tests/test_agents_client.py::test_api_key_initialization PASSED
tests/test_agents_client.py::test_initialization_failure_and_retry PASSED
tests/test_agents_client.py::test_reset_genai_client PASSED
6 passed in 1.94s

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.

bug: _GENAI_CLIENT singleton not thread-safe on cold start

1 participant