Repository navigation
test(29513): AsyncDisposableStack.disposeAsync awaits each resource #29564
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
farm/41279135/asyncdisposablestack-regression-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // https://github.com/oven-sh/bun/issues/29513 | ||
| // | ||
| // AsyncDisposableStack.prototype.disposeAsync() was not awaiting the promise | ||
| // returned from each resource's [Symbol.asyncDispose](). JavaScriptCore's | ||
| // GetDisposeMethod wrapped the method in a closure that called it for its | ||
| // side effects only, discarded the returned promise, and synchronously | ||
| // returned Promise.resolve(undefined). As a result, disposeAsync() resolved | ||
| // immediately (before any async cleanup finished) and resources were not | ||
| // disposed sequentially. | ||
| // | ||
| // Per https://tc39.es/proposal-explicit-resource-management/#sec-disposeresources, | ||
| // each resource's [Symbol.asyncDispose]() result must be awaited before the | ||
| // next resource (in LIFO order) is disposed. | ||
|
|
||
| import { expect, test } from "bun:test"; | ||
|
|
||
| test("AsyncDisposableStack.disposeAsync awaits each resource sequentially (use)", async () => { | ||
| const events: string[] = []; | ||
| const deferred: PromiseWithResolvers<void>[] = []; | ||
|
|
||
| class Resource implements AsyncDisposable { | ||
| constructor(public readonly label: string) {} | ||
| [Symbol.asyncDispose]() { | ||
| events.push(`begin ${this.label}`); | ||
| const d = Promise.withResolvers<void>(); | ||
| deferred.push(d); | ||
| return d.promise.then(() => { | ||
| events.push(`end ${this.label}`); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const stack = new AsyncDisposableStack(); | ||
| stack.use(new Resource("r1")); | ||
| stack.use(new Resource("r2")); | ||
| stack.use(new Resource("r3")); | ||
|
|
||
| let disposed = false; | ||
| const disposal = stack.disposeAsync().then(() => { | ||
| disposed = true; | ||
| }); | ||
|
|
||
| // disposeAsync() calls the first (LIFO: r3) disposer synchronously, so it | ||
| // has already begun. Nothing else should have started yet, and the overall | ||
| // promise must still be pending. | ||
| expect(events).toEqual(["begin r3"]); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| // Let r3 resolve. r2 should begin only after r3 has ended. | ||
| deferred[0].resolve(); | ||
| await Bun.sleep(0); | ||
| expect(events).toEqual(["begin r3", "end r3", "begin r2"]); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| // Let r2 resolve. r1 should begin only after r2 has ended. | ||
| deferred[1].resolve(); | ||
| await Bun.sleep(0); | ||
| expect(events).toEqual(["begin r3", "end r3", "begin r2", "end r2", "begin r1"]); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| // Let r1 resolve. Now disposeAsync() should resolve. | ||
| deferred[2].resolve(); | ||
| await disposal; | ||
|
|
||
| expect(events).toEqual(["begin r3", "end r3", "begin r2", "end r2", "begin r1", "end r1"]); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
|
|
||
| test("AsyncDisposableStack.disposeAsync awaits adopt() callbacks", async () => { | ||
| // adopt()'s wrapper closure had the same bug: it invoked the callback and | ||
| // dropped the returned promise on the floor. | ||
| const events: string[] = []; | ||
| const d1 = Promise.withResolvers<void>(); | ||
| const d2 = Promise.withResolvers<void>(); | ||
|
|
||
| const stack = new AsyncDisposableStack(); | ||
| stack.adopt("a", label => { | ||
| events.push(`begin ${label}`); | ||
| return d1.promise.then(() => { | ||
| events.push(`end ${label}`); | ||
| }); | ||
| }); | ||
| stack.adopt("b", label => { | ||
| events.push(`begin ${label}`); | ||
| return d2.promise.then(() => { | ||
| events.push(`end ${label}`); | ||
| }); | ||
| }); | ||
|
|
||
| let disposed = false; | ||
| const disposal = stack.disposeAsync().then(() => { | ||
| disposed = true; | ||
| }); | ||
|
|
||
| expect(events).toEqual(["begin b"]); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| d2.resolve(); | ||
| await Bun.sleep(0); | ||
| expect(events).toEqual(["begin b", "end b", "begin a"]); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| d1.resolve(); | ||
| await disposal; | ||
| expect(events).toEqual(["begin b", "end b", "begin a", "end a"]); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/24277 | ||
| // | ||
| // The same broken GetDisposeMethod implementation checked isCallable before | ||
| // checking for undefined/null, so passing a resource that only defined | ||
| // [Symbol.dispose] (no [Symbol.asyncDispose]) threw "@@asyncDispose must be | ||
| // callable" instead of falling back to the sync dispose method per | ||
| // https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod. | ||
| test("AsyncDisposableStack.use falls back to Symbol.dispose when Symbol.asyncDispose is absent", async () => { | ||
| const events: string[] = []; | ||
|
|
||
| const stack = new AsyncDisposableStack(); | ||
| stack.use({ | ||
| async [Symbol.asyncDispose]() { | ||
| events.push("async dispose"); | ||
| }, | ||
| }); | ||
| // Previously threw "TypeError: @@asyncDispose must be callable" at use(). | ||
| stack.use({ | ||
| [Symbol.dispose]() { | ||
| events.push("sync dispose"); | ||
| }, | ||
| }); | ||
|
|
||
| await stack.disposeAsync(); | ||
| expect(events).toEqual(["sync dispose", "async dispose"]); | ||
| }); | ||
|
|
||
| test("AsyncDisposableStack.disposeAsync awaits defer() callbacks", async () => { | ||
| const events: string[] = []; | ||
| const d = Promise.withResolvers<void>(); | ||
|
|
||
| const stack = new AsyncDisposableStack(); | ||
| stack.defer(() => { | ||
| events.push("begin"); | ||
| return d.promise.then(() => { | ||
| events.push("end"); | ||
| }); | ||
| }); | ||
|
|
||
| let disposed = false; | ||
| const disposal = stack.disposeAsync().then(() => { | ||
| disposed = true; | ||
| }); | ||
|
|
||
| expect(events).toEqual(["begin"]); | ||
| await Bun.sleep(0); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| d.resolve(); | ||
| await disposal; | ||
| expect(events).toEqual(["begin", "end"]); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.