Skip to content

Commit 2ba95ef

Browse files
committed
fix: race condition
1 parent 63df602 commit 2ba95ef

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/subscribe.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,25 @@ export const subscribe = (trigger: Trigger): Subscription => {
227227

228228
outerActiveTask.abortController.abort();
229229

230-
const abortedTaskPromise = outerActiveTask.promise;
230+
log.debug(
231+
'%s (%s): waiting for task to abort',
232+
trigger.name,
233+
outerActiveTask.id,
234+
);
235+
236+
if (outerActiveTask.queued) {
237+
return undefined;
238+
}
231239

232-
// Do not start a new task until the previous task has been
233-
// aborted and the shutdown routine has run to completion.
234-
await abortedTaskPromise;
240+
outerActiveTask.queued = true;
235241

236-
// eslint-disable-next-line require-atomic-updates
237-
outerActiveTask = null;
242+
try {
243+
// Do not start a new task until the previous task has been
244+
// aborted and the shutdown routine has run to completion.
245+
await outerActiveTask.promise;
246+
} catch {
247+
// nothing to do
248+
}
238249
} else {
239250
if (trigger.persistent) {
240251
log.warn(
@@ -287,7 +298,7 @@ export const subscribe = (trigger: Trigger): Subscription => {
287298
taskId,
288299
trigger,
289300
}) // eslint-disable-next-line promise/prefer-await-to-then
290-
.then(() => {
301+
.finally(() => {
291302
if (taskId === outerActiveTask?.id) {
292303
log.debug('%s (%s): completed task', trigger.name, taskId);
293304

0 commit comments

Comments
 (0)