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

[miniflare] fix: ensure Mutex doesn't report itself as drained if locked #4321

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions .changeset/rude-chicken-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"miniflare": patch
---

fix: ensure `Mutex` doesn't report itself as drained if locked

Previously, Miniflare's `Mutex` implementation would report itself as drained
if there were no waiters, regardless of the locked state. This bug meant that
if you called but didn't `await` `Miniflare#setOptions()`, future calls to
`Miniflare#dispatchFetch()` (or any other asynchronous `Miniflare` method)
wouldn't wait for the options update to apply and the runtime to restart before
sending requests. This change ensures we wait until the mutex is unlocked before
reporting it as drained.
2 changes: 1 addition & 1 deletion packages/miniflare/src/workers/shared/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Mutex {
}

async drained(): Promise<void> {
if (this.resolveQueue.length === 0) return;
if (this.resolveQueue.length === 0 && !this.locked) return;
return new Promise((resolve) => this.drainQueue.push(resolve));
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/miniflare/test/shared/sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test("Mutex: maintains separate drain queue", async (t) => {
void mutex.runWith(() => deferred1);
let drained = false;
mutex.drained().then(() => (drained = true));
await setTimeout();
mrbbot marked this conversation as resolved.
Show resolved Hide resolved
t.false(drained);
deferred1.resolve();
await setTimeout();
Expand All @@ -64,6 +65,7 @@ test("Mutex: maintains separate drain queue", async (t) => {
});
drained = false;
mutex.drained().then(() => (drained = true));
await setTimeout();
t.false(drained);
deferred2.resolve();
await setTimeout();
Expand Down
Loading