Skip to content

Commit ca72a3f

Browse files
author
PR Batch Tester
committed
fix(web): release queue locks granted after cancellation
1 parent dfdc648 commit ca72a3f

2 files changed

Lines changed: 48 additions & 40 deletions

File tree

‎apps/web/src/connection/threadTriageOutboxStore.test.ts‎

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -109,47 +109,53 @@ describe("browser thread triage storage", () => {
109109
}),
110110
);
111111

112-
it.effect("cancels a pending lock request before it can write", () =>
113-
Effect.gen(function* () {
114-
const requested = yield* Queue.unbounded<{
115-
signal: AbortSignal | undefined;
116-
grant: () => Promise<void>;
117-
}>();
118-
const setItem = vi.fn();
119-
vi.stubGlobal("window", { localStorage: { getItem: () => null, setItem } });
120-
vi.stubGlobal("navigator", {
121-
locks: {
122-
request: (
123-
_name: string,
124-
options: LockOptions | (() => Promise<unknown>),
125-
callback?: () => Promise<unknown>,
126-
) =>
127-
new Promise((resolve, reject) => {
128-
const signal = typeof options === "function" ? undefined : options.signal;
129-
const acquired = typeof options === "function" ? options : callback!;
130-
signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
131-
Queue.offerUnsafe(requested, {
132-
signal,
133-
grant: async () => {
134-
if (!signal?.aborted) resolve(await acquired());
135-
},
136-
});
137-
}),
138-
},
139-
});
112+
it.effect.each(["queued", "already granted"] as const)(
113+
"cancels a lock request before it can write (%s)",
114+
(state) =>
115+
Effect.gen(function* () {
116+
const requested = yield* Queue.unbounded<{
117+
signal: AbortSignal | undefined;
118+
grant: () => Promise<void>;
119+
}>();
120+
const setItem = vi.fn();
121+
vi.stubGlobal("window", { localStorage: { getItem: () => null, setItem } });
122+
vi.stubGlobal("navigator", {
123+
locks: {
124+
request: (
125+
_name: string,
126+
options: LockOptions | (() => Promise<unknown>),
127+
callback?: () => Promise<unknown>,
128+
) =>
129+
new Promise((resolve, reject) => {
130+
const signal = typeof options === "function" ? undefined : options.signal;
131+
const acquired = typeof options === "function" ? options : callback!;
132+
if (state === "queued") {
133+
signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
134+
}
135+
Queue.offerUnsafe(requested, {
136+
signal,
137+
grant: async () => {
138+
if (state === "already granted" || !signal?.aborted) resolve(await acquired());
139+
},
140+
});
141+
}),
142+
},
143+
});
140144

141-
const saving = yield* browserThreadTriageOutboxStore.put(queuedSettle).pipe(Effect.forkChild);
142-
const request = yield* Queue.take(requested);
143-
try {
144-
// A broken uninterruptible acquisition must still be released after the assertion.
145-
yield* Effect.sync(() => saving.interruptUnsafe());
146-
expect(request.signal?.aborted).toBe(true);
147-
} finally {
148-
yield* Effect.promise(request.grant);
149-
yield* Fiber.interrupt(saving);
150-
}
151-
expect(setItem).not.toHaveBeenCalled();
152-
}),
145+
const saving = yield* browserThreadTriageOutboxStore
146+
.put(queuedSettle)
147+
.pipe(Effect.forkChild);
148+
const request = yield* Queue.take(requested);
149+
try {
150+
// A broken uninterruptible acquisition must still be released after the assertion.
151+
yield* Effect.sync(() => saving.interruptUnsafe());
152+
expect(request.signal?.aborted).toBe(true);
153+
} finally {
154+
yield* Effect.promise(request.grant);
155+
yield* Fiber.interrupt(saving);
156+
}
157+
expect(setItem).not.toHaveBeenCalled();
158+
}),
153159
);
154160

155161
it.effect("uses the caller's logger during a locked storage read", () =>

‎apps/web/src/connection/threadTriageOutboxStore.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export const browserThreadTriageOutboxStore = makeThreadTriageOutboxStore({
4242
() =>
4343
new Promise<void>((release) => {
4444
lock.release = release;
45+
// Cancellation may finish before a granted lock's callback runs.
46+
if (lock.controller.signal.aborted) release();
4547
granted();
4648
}),
4749
)

0 commit comments

Comments
 (0)