Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added start/stop guards to running promise and serial queue #11120

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading