Skip to content

Conversation

@bastiandoetsch
Copy link
Contributor

Description

when multiple windows were opened and activated at the same time, global state was retrieved simultaneously, causing unintended duplicate execution of business logic.

Checklist

Screenshots / GIFs

Visuals that may help the reviewer. Please add screenshots for any UI change. GIFs are most welcome!

when multiple windows were opened and activated at the same time, global state was retrieved simultaneously, causing unintended duplicate execution of business logic.
@bastiandoetsch bastiandoetsch requested review from a team as code owners January 9, 2026 06:16
@snyk-io
Copy link

snyk-io bot commented Jan 9, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bastiandoetsch, 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 and resolves critical race conditions that could arise during the Snyk extension's activation, particularly when multiple VS Code windows were initialized simultaneously. The changes prevent unintended duplicate executions of core business logic, such as analytics event logging and modal displays, by introducing a controlled and safe mechanism for accessing and updating global state. This significantly enhances the extension's stability and ensures a consistent user experience across different VS Code window scenarios.

Highlights

  • Race Condition Fix: Implemented a robust fix for race conditions that occurred during extension activation, specifically when multiple VS Code windows were opened and activated concurrently.
  • Safe Global State Access: Introduced a new utility function, checkGlobalStateSafely, to manage access to global state values. This function prevents duplicate execution of logic by checking the window's focus state and incorporating a random delay to mitigate race conditions.
  • Refactoring for Modularity: Refactored the sendPluginInstalledEvent method to utilize the new checkGlobalStateSafely function and extracted the 'Secure at Inception' modal configuration into its own dedicated configureSecureAtInception method, improving code organization and readability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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
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 aims to fix a race condition during extension activation by checking for the focused window and adding a delay to state checks. While this is a step in the right direction, the use of a random delay is not a robust solution for preventing race conditions and can still lead to the issues it's trying to solve. I've identified a couple of areas for improvement to make the logic more resilient against race conditions, particularly around state updates for analytics events. My feedback includes specific suggestions to improve the atomicity of these operations.

Comment on lines +544 to +546
analyticsSender.logEvent(pluginInstalledEvent, () => {
void extensionContext.updateGlobalStateValue(MEMENTO_ANALYTICS_PLUGIN_INSTALLED_SENT, true);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The MEMENTO_ANALYTICS_PLUGIN_INSTALLED_SENT global state is updated within the callback for analyticsSender.logEvent. Since logEvent queues the event for asynchronous sending, there's a significant delay before the callback is executed and the state is updated. During this time, another extension instance could activate, check the state (which would still be false), and also queue a 'plugin installed' event, which can lead to duplicate analytics.

To fix this, you should update the global state immediately before queueing the analytics event. This pattern is correctly used for MEMENTO_SECURE_AT_INCEPTION_MODAL in the configureSecureAtInception method and should be applied here as well for consistency and correctness.

Suggested change
analyticsSender.logEvent(pluginInstalledEvent, () => {
void extensionContext.updateGlobalStateValue(MEMENTO_ANALYTICS_PLUGIN_INSTALLED_SENT, true);
});
await extensionContext.updateGlobalStateValue(MEMENTO_ANALYTICS_PLUGIN_INSTALLED_SENT, true);
analyticsSender.logEvent(pluginInstalledEvent, () => {
// State is updated before queueing, so this callback can be a no-op.
});

Comment on lines +565 to +566
// random wait to lower chance of race conditions
await new Promise(resolve => setTimeout(resolve, Math.random() * 200));
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Using a random delay to mitigate race conditions is a fragile approach and doesn't guarantee that the race condition is prevented; it only makes it less likely to occur. If two extension instances in different windows both pass the initial checks and their random delays are short and similar, they might still both read the old state and proceed, leading to the duplicate execution this PR aims to fix.

A more robust approach would be to use a proper locking mechanism, though the VS Code API lacks built-in primitives for this. A simpler and more effective pattern is to ensure check-and-set operations are as atomic as possible by updating the state before initiating the one-time action, as you've done in configureSecureAtInception.

@bastiandoetsch bastiandoetsch force-pushed the fix/race-condition-in-snyk-studio-enablement branch from e487815 to 2a9a90e Compare January 9, 2026 08:31
@bastiandoetsch bastiandoetsch enabled auto-merge (squash) January 9, 2026 08:32
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