Skip to content

Commit b0410a4

Browse files
authored
fix(browser): serialize concurrent profile clears (pingdotgg#23434)
1 parent 2b1e15c commit b0410a4

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎apps/server/src/preview/ServerBrowserContexts.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ describe("ServerBrowserContexts", () => {
183183
await pool.close();
184184
});
185185

186+
it("waits for the context to close when the same profile is cleared concurrently", async () => {
187+
const human = makeContext();
188+
const closed = Promise.withResolvers<void>();
189+
human.close.mockImplementation(async () => closed.promise);
190+
launches.persistent.mockResolvedValue(human as unknown as BrowserContext);
191+
const pool = new ServerBrowserContexts(options());
192+
await pool.contextFor("work/team");
193+
194+
const first = pool.clearProfile("work/team");
195+
const second = pool.clearProfile("work/team");
196+
const deletionsBeforeClose = launches.rm.mock.calls.length;
197+
closed.resolve();
198+
await Promise.all([first, second]);
199+
200+
expect(deletionsBeforeClose).toBe(0);
201+
expect(human.close).toHaveBeenCalledTimes(1);
202+
expect(launches.rm).toHaveBeenCalledExactlyOnceWith("/test/profiles/work%2Fteam", {
203+
recursive: true,
204+
force: true,
205+
});
206+
await pool.close();
207+
});
208+
186209
it("opens a profile requested during its clear only after the storage is deleted", async () => {
187210
launches.persistent.mockImplementation(async () => makeContext() as unknown as BrowserContext);
188211
const configuration = options();

‎apps/server/src/preview/ServerBrowserContexts.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ export class ServerBrowserContexts {
212212
async clearProfile(profileId: string) {
213213
if (profileId === INCOGNITO_BROWSER_PROFILE_ID) return;
214214
const key = JSON.stringify([profileId, null]);
215+
const existing = this.clearing.get(key);
216+
if (existing) return existing;
215217
const clearing = (async () => {
216218
const pending = this.contexts.get(key);
217219
if (pending) {

0 commit comments

Comments
 (0)