Skip to content

refactor: remove redundant initialized flag from DatabaseSessionService#660

Open
MarvelNwachukwu wants to merge 2 commits intomainfrom
review-db-session-dedup
Open

refactor: remove redundant initialized flag from DatabaseSessionService#660
MarvelNwachukwu wants to merge 2 commits intomainfrom
review-db-session-dedup

Conversation

@MarvelNwachukwu
Copy link
Copy Markdown
Contributor

Pull Request

Description

Removes the redundant initialized boolean from DatabaseSessionService. The initPromise field already tracks initialization state — a non-null resolved promise serves the same purpose as initialized = true. This simplifies the deduplication logic introduced in #646.

Feedback from code review by @0x_Snake.

Related Issue

Follow-up to #646

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)
  • Tests
  • Other (please describe):

How Has This Been Tested?

initialized is purely internal to DatabaseSessionService — only referenced in 3 places within the same file, all of which are replaced by initPromise checks. No behavioral change.

Checklist

  • My code follows the code style of this project
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • My changes generate no new warnings
  • I have checked for potential breaking changes and addressed them

The initPromise field already tracks initialization state, making the
separate boolean flag unnecessary. Simplifies the deduplication logic.
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 26, 2026

🦋 Changeset detected

Latest commit: a8d7163

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@iqai/adk Patch
@iqai/adk-cli Patch
@iqai/mcp-docs Patch

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

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
adk-typescript-docs Ready Ready Preview Mar 26, 2026 8:34am
adk-web Ready Ready Preview Mar 26, 2026 8:34am

Request Review

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 refactors the DatabaseSessionService by removing a redundant initialized boolean flag. The service's initialization state is now solely determined by the presence and resolution of the initPromise, simplifying the internal logic and improving code clarity without altering external behavior. This change addresses feedback from a previous review and streamlines the promise deduplication mechanism.

Highlights

  • Redundant Flag Removal: Eliminated the initialized boolean flag from DatabaseSessionService, as its state was already implicitly managed by the initPromise.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the database session initialization logic by removing the initialized flag and streamlining the initializeDatabase and ensureInitialized methods to rely solely on the initPromise for promise deduplication. However, a critical race condition has been identified in the initializeDatabase method: if an initialization promise fails, its .catch() handler might incorrectly nullify a new initPromise if a subsequent initialization starts before the catch block executes, potentially leading to redundant initializations. The original logic for clearing initPromise on failure was more robust and should be reinstated.

Comment on lines +101 to +108
this.initPromise = this._doInitialize();

// 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;
}
this.initPromise.catch(() => {
this.initPromise = null;
});

return promise;
return this.initPromise;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This implementation introduces a race condition when handling initialization failures. If an initialization promise fails, and a new initialization starts before the .catch() handler of the failed promise executes, the handler will incorrectly nullify the initPromise of the new initialization. This can lead to unexpected behavior and redundant initializations.

To fix this, you should capture the promise in a local variable and check if this.initPromise still refers to the same failed promise within the .catch() block before setting it to null. This was the behavior in the previous implementation and is crucial for correctness.

    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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the defensive guard. In practice, JS's single-threaded microtask ordering means our .catch() (registered first) always fires before any caller's rejection handler could retry and reassign initPromise. So the race described can't actually occur in JS's event loop.

That said, the identity check is cheap and makes the intent explicit — restored it in a8d7163.

Restore the identity check in the .catch() handler to guard against
theoretical race conditions. Add missing changeset for the PR.
@MarvelNwachukwu
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the DatabaseSessionService by removing a redundant initialized flag and its associated checks, streamlining the database initialization process to rely solely on the initPromise for deduplication. The reviewer suggests restoring a comment explaining the purpose of the if (this.initPromise === promise) check in the initializeDatabase method's error handling, as its removal could negatively impact maintainability and potentially lead to future race condition bugs.

// Clear the promise on failure to allow retries.
promise.catch(() => {
// Avoid race conditions if a new initialization has started.
if (this.initPromise === promise) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment explaining this conditional check was removed. This check is crucial for handling a subtle race condition, and its purpose is not immediately obvious. Removing the comment harms maintainability, as a future developer might not understand its importance and could mistakenly remove it, introducing a bug.

Please restore a comment explaining why this check is necessary.

			// This check prevents a race condition where a new initialization may have started
			// before this catch block executes for a failed one.
			if (this.initPromise === promise) {

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