fix: make getTopLevelDomain and isEnabled synchronous to avoid re-entrancy#1564
Merged
daniel-graham-amplitude merged 76 commits intomainfrom Mar 5, 2026
Merged
fix: make getTopLevelDomain and isEnabled synchronous to avoid re-entrancy#1564daniel-graham-amplitude merged 76 commits intomainfrom
daniel-graham-amplitude merged 76 commits intomainfrom
Conversation
Collaborator
Author
|
bugbot run |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix prepared fixes for both issues found in the latest run.
- ✅ Fixed: Wrong option name in test file is silently ignored
- Replaced the invalid
_useExperimentalMutexoption with_enableNextFeaturesin the test server init config so the flag is actually applied.
- Replaced the invalid
- ✅ Fixed: Static
_enableNextFeaturesflag creates race condition between instances- Removed per-instance mutation of the static flag in
useBrowserConfigand threaded an instance-localenableNextFeaturesvalue throughgetTopLevelDomainandCookieStorage.isEnabledto avoid cross-instance races.
- Removed per-instance mutation of the static flag in
Or push these changes by commenting:
@cursor push b53a95ee88
Preview (b53a95ee88)
diff --git a/packages/analytics-browser/src/config.ts b/packages/analytics-browser/src/config.ts
--- a/packages/analytics-browser/src/config.ts
+++ b/packages/analytics-browser/src/config.ts
@@ -295,8 +295,7 @@
diagnosticsClient?: IDiagnosticsClient,
earlyConfig?: EarlyConfig,
): Promise<IBrowserConfig> => {
- // set the experimental mutex flag to enable locking in CookieStorage
- CookieStorage._enableNextFeatures = options._enableNextFeatures || false;
+ const enableNextFeatures = options._enableNextFeatures || false;
// Step 1: Create identity storage instance
const identityStorage = options.identityStorage || DEFAULT_IDENTITY_STORAGE;
@@ -305,7 +304,7 @@
// use the getTopLevelDomain function to find the TLD only if identity storage
// is cookie (because getTopLevelDomain() uses cookies)
if (identityStorage === DEFAULT_IDENTITY_STORAGE) {
- defaultCookieDomain = await getTopLevelDomain(undefined, diagnosticsClient);
+ defaultCookieDomain = await getTopLevelDomain(undefined, diagnosticsClient, enableNextFeatures);
}
const cookieOptions = {
domain: options.cookieOptions?.domain ?? defaultCookieDomain,
@@ -516,14 +515,18 @@
return '';
};
-export const getTopLevelDomain = async (url?: string, diagnosticsClient?: IDiagnosticsClient) => {
+export const getTopLevelDomain = async (
+ url?: string,
+ diagnosticsClient?: IDiagnosticsClient,
+ enableNextFeatures = CookieStorage._enableNextFeatures,
+) => {
if (
- !(await new CookieStorage<number>(undefined, { diagnosticsClient }).isEnabled()) ||
+ !(await new CookieStorage<number>(undefined, { diagnosticsClient }).isEnabled(enableNextFeatures)) ||
(!url && (typeof location === 'undefined' || !location.hostname))
) {
return '';
}
- if (CookieStorage._enableNextFeatures) {
+ if (enableNextFeatures) {
return getTopLevelDomainSync(url, diagnosticsClient);
}
diff --git a/packages/analytics-core/src/storage/cookie.ts b/packages/analytics-core/src/storage/cookie.ts
--- a/packages/analytics-core/src/storage/cookie.ts
+++ b/packages/analytics-core/src/storage/cookie.ts
@@ -67,7 +67,7 @@
}
}
- async isEnabled(): Promise<boolean> {
+ async isEnabled(enableNextFeatures = CookieStorage._enableNextFeatures): Promise<boolean> {
const globalScope = getGlobalScope();
/* istanbul ignore if */
if (!globalScope || !globalScope.document) {
@@ -75,7 +75,7 @@
}
// experimental feature for now that uses navigator.locks
- if (CookieStorage._enableNextFeatures) {
+ if (enableNextFeatures) {
return this.isEnabledSync();
}
diff --git a/test-server/autocapture/element-interactions.html b/test-server/autocapture/element-interactions.html
--- a/test-server/autocapture/element-interactions.html
+++ b/test-server/autocapture/element-interactions.html
@@ -273,7 +273,7 @@
import.meta.env.VITE_AMPLITUDE_API_KEY,
import.meta.env.VITE_AMPLITUDE_USER_ID || 'amplitude-typescript test user',
{
- _useExperimentalMutex: true,
+ _enableNextFeatures: true,
fetchRemoteConfig: false,
logLevel: 'debug',
autocapture: {35252cb to
d244673
Compare
d244673 to
91e5baf
Compare
…com:amplitude/Amplitude-TypeScript into AMP-149016-is-enabled-v2
…com:amplitude/Amplitude-TypeScript into AMP-149016-is-enabled-v2
…com:amplitude/Amplitude-TypeScript into AMP-149016-is-enabled-v2
…com:amplitude/Amplitude-TypeScript into AMP-149016-is-enabled-v2
crleona
approved these changes
Mar 5, 2026
Base automatically changed from
AMP-149016-cookie-transaction-helper-method
to
main
March 5, 2026 18:03
… AMP-149016-is-enabled-v2
Co-authored-by: Xinyi Ye <xinyi.ye@amplitude.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
(follow-up PR after #1569)
"isEnabled" and "getTopLevelDomain" both operate on 1. write cookie; 2. read cookie; 3. delete cookie
These operations are problematic if done concurrently. Our current solution we add a UUID to the cookie name to avoid collisions, but this results in more cookies being created than necessary, and seemingly, these cookies sometimes are rejected by the browser.
What this does is does away with the UUID scoping, and back to the hardcoded names. But now it wraps the operation in a "transaction" (as introduced by #1569) so that when this transaction is run, all other instances (including across tabs) are locked from reading and writing the cookie until the transaction is done.
Checklist
Note
Medium Risk
Changes core cookie write/read/cleanup paths and domain-scoping detection; failures could impact cookie persistence and session identity, especially in browsers without
navigator.locksor with strict cookie policies.Overview
Updates browser TLD detection to stop creating per-call UUID-namespaced test cookies and instead rely on
CookieStorage.isDomainWritable()for checking which parent domain can accept cookies.Refactors
CookieStorage.isEnabled()to run the write/read/cleanup test inside the new cookietransactionlock with a stableAMP_TESTkey, and adjusts tests and thetest-serverstress page (RUNS 10→1000) to validate the new concurrency-safe behavior.Written by Cursor Bugbot for commit 214f0e1. This will update automatically on new commits. Configure here.