From 23e642f85009a3a4779bc762cf36484771014b57 Mon Sep 17 00:00:00 2001 From: PhilWindle <60546371+PhilWindle@users.noreply.github.com> Date: Thu, 9 Jan 2025 13:16:54 +0000 Subject: [PATCH] fix: Added start/stop guards to running promise and serial queue (#11120) This PR adds guards to `SerialQueue` and `RunningPromise` so they can't inadvertantly be 'started' multiple times. --- yarn-project/foundation/src/promise/running-promise.ts | 8 ++++++++ yarn-project/foundation/src/queue/serial_queue.ts | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/yarn-project/foundation/src/promise/running-promise.ts b/yarn-project/foundation/src/promise/running-promise.ts index 69288fcb0ca..093f572d371 100644 --- a/yarn-project/foundation/src/promise/running-promise.ts +++ b/yarn-project/foundation/src/promise/running-promise.ts @@ -23,6 +23,10 @@ export class RunningPromise { * Starts the running promise. */ public start() { + if (this.running) { + this.logger.warn(`Attempted to start running promise that was already started`); + return; + } this.running = true; const poll = async () => { @@ -54,6 +58,10 @@ export class RunningPromise { * and waits for the currently executing function to complete. */ async stop(): Promise { + if (!this.running) { + this.logger.warn(`Running promise was not started`); + return; + } this.running = false; this.interruptibleSleep.interrupt(); await this.runningPromise; diff --git a/yarn-project/foundation/src/queue/serial_queue.ts b/yarn-project/foundation/src/queue/serial_queue.ts index b6bcd9baadd..c00e565de80 100644 --- a/yarn-project/foundation/src/queue/serial_queue.ts +++ b/yarn-project/foundation/src/queue/serial_queue.ts @@ -6,6 +6,7 @@ import { FifoMemoryQueue } from './fifo_memory_queue.js'; export class SerialQueue { private readonly queue = new FifoMemoryQueue<() => Promise>(); private runningPromise!: Promise; + private started = false; /** * Initializes the execution of enqueued functions in the serial queue. @@ -14,7 +15,11 @@ export class SerialQueue { * This method should be called once to start processing the queue. */ public start() { + if (this.started) { + return; + } this.runningPromise = this.queue.process(fn => fn()); + this.started = true; } /**