-
Notifications
You must be signed in to change notification settings - Fork 31
fix: races during extension activation #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
when multiple windows were opened and activated at the same time, global state was retrieved simultaneously, causing unintended duplicate execution of business logic.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Summary of ChangesHello @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
🧠 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 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. 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
|
There was a problem hiding this 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.
| analyticsSender.logEvent(pluginInstalledEvent, () => { | ||
| void extensionContext.updateGlobalStateValue(MEMENTO_ANALYTICS_PLUGIN_INSTALLED_SENT, true); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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. | |
| }); |
| // random wait to lower chance of race conditions | ||
| await new Promise(resolve => setTimeout(resolve, Math.random() * 200)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
e487815 to
2a9a90e
Compare
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!