fix: cache VectorSearchClient across Client calls - #15
Conversation
82ffa34 to
14bccad
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| let client = try await VectorSearchClient(databaseDirectoryURL: databaseDirectoryURL, readOnly: true) | ||
| cachedSearchClient = client | ||
| return client | ||
| } |
There was a problem hiding this comment.
Race condition in lazy client caching across suspension points
Medium Severity
The searchClient() method has a race condition: the nil check on cachedSearchClient at the top is separated from the assignment by await suspension points (DocumentationAssetLocator and VectorSearchClient init). Concurrent callers can both observe nil, both create a new VectorSearchClient, and both write to the property — defeating the caching goal. The PR description mentions a SearchClientCache actor to solve this, but no such actor exists in the code. Without actor isolation or another synchronization mechanism, the check-then-set pattern is not safe across suspension points.
14bccad to
d701866
Compare


VectorSearchClientinstance acrosssearch()andfetch()calls onClientusing a private actor, avoiding redundantDocumentationAssetLocatorresolution andVectorSearchClientinitialization on every call.VectorSearchClientasSendableso it can be safely shared across isolation boundaries.SearchClientCacheactor lazily creates the client on first use and reuses it for all subsequent calls.Note
Medium Risk
Introduces shared
VectorSearchClientstate insideClient, which could affect concurrency/thread-safety and resource lifetime ifClientis used from multiple tasks simultaneously.Overview
Clientnow lazily caches a singleVectorSearchClientinstance and reuses it acrosssearch()andfetch()calls, avoiding repeatedDocumentationAssetLocatorlookups and client initialization.Adds a private
searchClient()helper that creates the client on first use and returns the cached instance thereafter.Written by Cursor Bugbot for commit d701866. This will update automatically on new commits. Configure here.