Skip to content

Commit 22e38ee

Browse files
committed
fix(browser): serialize concurrent profile clears
1 parent 3b95e78 commit 22e38ee

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
@@ -137,6 +137,29 @@ describe("ServerBrowserContexts", () => {
137137
await pool.close();
138138
});
139139

140+
it("waits for the context to close when the same profile is cleared concurrently", async () => {
141+
const human = makeContext();
142+
const closed = Promise.withResolvers<void>();
143+
human.close.mockImplementation(async () => closed.promise);
144+
launches.persistent.mockResolvedValue(human as unknown as BrowserContext);
145+
const pool = new ServerBrowserContexts(options());
146+
await pool.contextFor("work/team");
147+
148+
const first = pool.clearProfile("work/team");
149+
const second = pool.clearProfile("work/team");
150+
const deletionsBeforeClose = launches.rm.mock.calls.length;
151+
closed.resolve();
152+
await Promise.all([first, second]);
153+
154+
expect(deletionsBeforeClose).toBe(0);
155+
expect(human.close).toHaveBeenCalledTimes(1);
156+
expect(launches.rm).toHaveBeenCalledExactlyOnceWith("/test/profiles/work%2Fteam", {
157+
recursive: true,
158+
force: true,
159+
});
160+
await pool.close();
161+
});
162+
140163
it("opens a profile requested during its clear only after the storage is deleted", async () => {
141164
launches.persistent.mockImplementation(async () => makeContext() as unknown as BrowserContext);
142165
const configuration = options();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ export class ServerBrowserContexts {
196196
async clearProfile(profileId: string) {
197197
if (profileId === INCOGNITO_BROWSER_PROFILE_ID) return;
198198
const key = JSON.stringify([profileId, null]);
199+
const existing = this.clearing.get(key);
200+
if (existing) return existing;
199201
const clearing = (async () => {
200202
const pending = this.contexts.get(key);
201203
if (pending) {

0 commit comments

Comments
 (0)