Skip to content

feat: Add Symbol.dispose and Symbol.asyncDispose support to DispatcherBase #4258

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PandaWorker
Copy link

@PandaWorker PandaWorker commented Jun 2, 2025

This PR implements the new ECMAScript Explicit Resource Management proposal by adding disposal functionality to the DispatcherBase class. The changes include:

  1. Added [Symbol.asyncDispose]() method that asynchronously closes the dispatcher
  2. Added [Symbol.dispose]() method that synchronously closes the dispatcher
  3. Both methods reuse the existing close() functionality
  4. The synchronous version uses the noop function as a callback

These changes enable modern resource management patterns:

// Synchronous disposal
using dispatcher = new Agent();

// Async disposal
await using asyncDispatcher = new Client();

The implementation follows the TC39 proposal currently in Stage 3 (https://github.com/tc39/proposal-explicit-resource-management)

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests to it?

@mcollina
Copy link
Member

mcollina commented Jun 3, 2025

I think this is a good start. You should make sure tha the result of .compose() can be disposed.

Also, docs are missing.

@PandaWorker
Copy link
Author

I'm also confused about what 's after .the close() agent no longer accepts requests, I think we need to completely not accept requests only at .destroy(), or add a method .closeIdleConnections(), which will simply wait for requests to complete and close all connections.

@mcollina
Copy link
Member

mcollina commented Jun 3, 2025

I'm also confused about what 's after .the close() agent no longer accepts requests

I agree, I think it should error at this point but not drop any current requests.

Comment on lines +164 to +167

[Symbol.dispose] () {
this.close(noop)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Symbol.dispose] () {
this.close(noop)
}

Queueing a callback does not equate to a synchronous task. The semantics of explicit resource management are such that by the time the disposer returns (or the async disposer's Promise is resolved), the resource disposal should be complete, to maintain the expected order of operations; this is not the case here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t believe there’s much else we can do for this; the close method is async by default, we cannot ensure sync guarantees.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, hence only the asyncDispose method makes sense here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants