Skip to content

Commit

Permalink
fix: Added start/stop guards to running promise and serial queue (#11120
Browse files Browse the repository at this point in the history
)

This PR adds guards to `SerialQueue` and `RunningPromise` so they can't
inadvertantly be 'started' multiple times.
  • Loading branch information
PhilWindle authored Jan 9, 2025
1 parent 39535c9 commit 23e642f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions yarn-project/foundation/src/promise/running-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -54,6 +58,10 @@ export class RunningPromise {
* and waits for the currently executing function to complete.
*/
async stop(): Promise<void> {
if (!this.running) {
this.logger.warn(`Running promise was not started`);
return;
}
this.running = false;
this.interruptibleSleep.interrupt();
await this.runningPromise;
Expand Down
5 changes: 5 additions & 0 deletions yarn-project/foundation/src/queue/serial_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FifoMemoryQueue } from './fifo_memory_queue.js';
export class SerialQueue {
private readonly queue = new FifoMemoryQueue<() => Promise<void>>();
private runningPromise!: Promise<void>;
private started = false;

/**
* Initializes the execution of enqueued functions in the serial queue.
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 23e642f

Please sign in to comment.