Skip to content

Conversation

@fulleni
Copy link
Member

@fulleni fulleni commented Oct 19, 2025

Status

READY

Description

This pull request addresses a potential race condition in the application's dependency initialization process. By introducing a Completer, it ensures that the init() method is idempotent, meaning dependencies are initialized only once, even if called multiple times concurrently. This change enhances the robustness and reliability of the application's startup sequence by providing a controlled and single-threaded initialization flow.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Refactors the `AppDependencies` singleton to use a `Completer` for managing the asynchronous initialization process.

This change introduces an atomic, thread-safe mechanism that ensures the core initialization logic runs exactly once, even when multiple concurrent requests trigger the `init()` method during server startup or hot reload.

The previous implementation was vulnerable to a race condition where a second request could bypass the `_isInitialized` check before the first request had completed, leading to a `LateInitializationError`. The new `Completer`-based approach solves this by having all concurrent callers `await` the same `Future`, guaranteeing a single execution.
Resolves the `unawaited_futures` lint warning by wrapping the call to `_initializeDependencies()` with `unawaited()`.

This makes it explicit that the future is intentionally not being awaited at this call site, as the logic is correctly handled by the `_initCompleter`.
…ation

- Add guard to ensure only one instance of dependency initialization runs at a time
- Update CHANGELOG.md to reflect the upcoming fix
@gemini-code-assist
Copy link

Summary of Changes

Hello @fulleni, 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 addresses a potential race condition in the application's dependency initialization process. By introducing a Completer, it ensures that the init() method is idempotent, meaning dependencies are initialized only once, even if called multiple times concurrently. This change enhances the robustness and reliability of the application's startup sequence by providing a controlled and single-threaded initialization flow.

Highlights

  • Race Condition Fix: Implemented a robust mechanism using a Completer to prevent race conditions that could occur during concurrent calls to the application's dependency initialization method.
  • Idempotent Initialization: Ensured that the init() method for application dependencies is idempotent, meaning the core initialization logic will execute only once, even if the method is invoked multiple times concurrently.
  • Refactored Initialization Logic: Separated the core dependency initialization steps into a new private method, _initializeDependencies(), improving code organization and clarity within the AppDependencies class.
  • Improved State Management: Replaced manual boolean flags and error storage with the Completer's state, providing a more streamlined and reliable way to track initialization progress and outcomes.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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

@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 effectively resolves a race condition during concurrent dependency initialization by introducing a Completer to manage the asynchronous setup. The approach is sound. However, I've identified a new potential race condition in the dispose method that could arise if it's called while initialization is still running. My review includes a high-severity fix for this issue. I've also provided a couple of medium-severity suggestions for code style and changelog consistency.

Updates the `dispose` method in `AppDependencies` to await the completion of the `_initCompleter` before proceeding with resource cleanup.

This change prevents a race condition where `dispose()` could be called while `_initializeDependencies()` is still executing, which would lead to unpredictable errors from tearing down resources that are actively being initialized. The `dispose` method now safely waits for initialization to finish, whether it succeeded or failed, ensuring a clean and predictable shutdown sequence.
Remove 'async' from the 'init' method in AppDependencies class.
The method no longer contains 'await' calls, making the 'async'
keyword redundant. This change slightly optimizes the method
definition without altering its functionality.
- Change single dash to double asterisks for fix description in upcoming release section
@fulleni fulleni merged commit f6e64dc into main Oct 19, 2025
1 check failed
@fulleni fulleni deleted the fix/init branch October 19, 2025 09:22
@fulleni fulleni added this to the Foundation Edition milestone Nov 1, 2025
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.

2 participants