Repository navigation
make GenAI client initialization thread-safe (#82) - #110
Open
Arunpurohit1766 wants to merge 1 commit into
Open
Arunpurohit1766 wants to merge 1 commit into
Arunpurohit1766 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
Resolves #82 by implementing thread-safe lazy initialization for the
_GENAI_CLIENTsingleton inbackend/agents.pyusing 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 Noneas true, leading to redundant client constructions, orphaned connection pools, and memory leaks.Implementation Details
threading.Lock(_CLIENT_LOCK). Warm requests return immediately without acquiring the lock (zero performance penalty), while cold requests synchronize before initializing the client._GENAI_CLIENTis assigned only after successful client construction. If construction fails (e.g. invalid credentials or transient network error), the singleton remainsNoneand the lock is cleanly released, allowing subsequent requests to retry._reset_genai_client()for test isolation. It safely callsclient.close()if exposed by the installed SDK to release active sockets before resetting the reference.GCP_PROJECT_ID,GCP_REGION,GOOGLE_API_KEY), and default values.Verification
Created
tests/test_agents_client.pycovering 6 regression scenarios:test_concurrent_cold_start_singleton: Synchronizes 20 worker threads viathreading.Barrierhitting cold start simultaneously. Asserts constructor is called exactly once and all threads receive the exact same instance.test_warm_start_singleton: Asserts cached instance is returned without constructor re-invocation.test_vertex_ai_initialization: Asserts Vertex arguments and regions are preserved.test_api_key_initialization: Asserts API-key mode correctly passes credentials.test_initialization_failure_and_retry: Asserts state recovery after transient constructor errors.test_reset_genai_client: Asserts.close()is invoked and state resets cleanly.All 6 tests pass in pytest: