fix: deduplicate concurrent database session initialization#646
fix: deduplicate concurrent database session initialization#646MarvelNwachukwu merged 2 commits intomainfrom
Conversation
The constructor fired initializeDatabase() without deduplication, so concurrent callers via ensureInitialized() could trigger duplicate initializations. Cache the init promise so all callers share a single initialization, with retry on failure.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: d25219b The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical race condition within the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request fixes a race condition in DatabaseSessionService where concurrent initializations could occur. The fix introduces promise deduplication by caching the initialization promise, which is a solid approach. My review includes a suggestion to refactor the error handling logic slightly to improve separation of concerns, making the code more modular and maintainable. Overall, the change is correct and effectively solves the described issue.
| this.initPromise = this._doInitialize(); | ||
| return this.initPromise; |
There was a problem hiding this comment.
For better separation of concerns, the logic for clearing initPromise on failure could be handled here in initializeDatabase rather than inside _doInitialize. This would make _doInitialize responsible only for the initialization logic, improving modularity. This change should be paired with removing the corresponding line from the catch block in _doInitialize (line 199).
const promise = this._doInitialize();
this.initPromise = promise;
// Clear the promise on failure to allow retries.
promise.catch(() => {
// Avoid race conditions if a new initialization has started.
if (this.initPromise === promise) {
this.initPromise = null;
}
});
return promise;|
|
||
| this.initialized = true; | ||
| } catch (error) { | ||
| this.initPromise = null; // Allow retry on failure |
Move the initPromise clearing logic from _doInitialize's catch block into initializeDatabase, with a guard to avoid clearing a newer initialization's promise. Also adds changeset for the patch.
|
@claude review |
|
Claude finished @MarvelNwachukwu's task —— View job Code Review for Database Session Initialization Race Condition FixTodo List:
AnalysisThe race condition fix in Problem Identified: The original constructor fired Solution Overview: The fix introduces promise-based deduplication by caching the initialization promise in Key Changes Review✅ Strong Points:
Detailed Technical ReviewPromise Deduplication Logic (Lines 97-117): private initializeDatabase(): Promise<void> {
if (this.initialized) {
return Promise.resolve();
}
if (this.initPromise) {
return this.initPromise; // ✅ Proper deduplication
}
const promise = this._doInitialize();
this.initPromise = promise;
promise.catch(() => {
if (this.initPromise === promise) { // ✅ Prevents race conditions
this.initPromise = null;
}
});
return promise;
}This is a textbook implementation of promise deduplication. The logic correctly handles:
Testing Considerations:
Comparison with Gemini's SuggestionsGemini suggested moving the error handling logic to The current implementation already follows good separation of concerns with Final AssessmentVerdict: ✅ Approve - This is a solid fix that effectively resolves the race condition Impact: Low risk, high value bug fix that prevents duplicate database operations The implementation correctly solves the described race condition with minimal code changes and maintains all existing functionality. |
Pull Request
Description
The
DatabaseSessionServiceconstructor firesinitializeDatabase()as a fire-and-forget async call without deduplication. Concurrent callers (viaensureInitialized()) could pass thethis.initializedboolean guard before either completes, triggering duplicate table creation work. This fix caches the initialization promise ininitPromiseso all concurrent callers share a single initialization. On failure, the cached promise is cleared to allow retry.Related Issue
Closes #647
Type of Change
How Has This Been Tested?
pnpm test --filter=@iqai/adk)pnpm build --filter=@iqai/adk)Checklist