TypeScript
fix(browser): serialize concurrent profile clears (#23434)
Aditya Garudb0410a445397
diff --git a/apps/server/src/preview/ServerBrowserContexts.test.ts b/apps/server/src/preview/ServerBrowserContexts.test.tsindex 3cab864de54..522df30fb66 100644--- a/apps/server/src/preview/ServerBrowserContexts.test.ts+++ b/apps/server/src/preview/ServerBrowserContexts.test.ts@@ -183,6 +183,29 @@ describe("ServerBrowserContexts", () => {await pool.close();});+ it("waits for the context to close when the same profile is cleared concurrently", async () => {+ const human = makeContext();+ const closed = Promise.withResolvers<void>();+ human.close.mockImplementation(async () => closed.promise);+ launches.persistent.mockResolvedValue(human as unknown as BrowserContext);+ const pool = new ServerBrowserContexts(options());+ await pool.contextFor("work/team");++ const first = pool.clearProfile("work/team");+ const second = pool.clearProfile("work/team");+ const deletionsBeforeClose = launches.rm.mock.calls.length;+ closed.resolve();+ await Promise.all([first, second]);++ expect(deletionsBeforeClose).toBe(0);+ expect(human.close).toHaveBeenCalledTimes(1);+ expect(launches.rm).toHaveBeenCalledExactlyOnceWith("/test/profiles/work%2Fteam", {+ recursive: true,+ force: true,+ });+ await pool.close();+ });+it("opens a profile requested during its clear only after the storage is deleted", async () => {launches.persistent.mockImplementation(async () => makeContext() as unknown as BrowserContext);const configuration = options();diff --git a/apps/server/src/preview/ServerBrowserContexts.ts b/apps/server/src/preview/ServerBrowserContexts.tsindex 9f3fd484307..2a7c342d30d 100644--- a/apps/server/src/preview/ServerBrowserContexts.ts+++ b/apps/server/src/preview/ServerBrowserContexts.ts@@ -212,6 +212,8 @@ export class ServerBrowserContexts {async clearProfile(profileId: string) {if (profileId === INCOGNITO_BROWSER_PROFILE_ID) return;const key = JSON.stringify([profileId, null]);+ const existing = this.clearing.get(key);+ if (existing) return existing;const clearing = (async () => {const pending = this.contexts.get(key);if (pending) {