Skip to content

Is it possible to hook into the moment a promise object is clean up? #1037

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

Closed
nabetti1720 opened this issue Apr 27, 2025 · 2 comments
Closed

Comments

@nabetti1720
Copy link

nabetti1720 commented Apr 27, 2025

Related #1030
Related awslabs/llrt#947

--
We are currently working on developing a nodejs compatible async_hooks module for LLRT.

We are getting close to completing the basic implementation, but I'm having trouble figuring out when to discard the ID information stored on the Rust side.

In the current implementation, the ID information created at the Init event is deleted when the resolve event occurs in the Promise Hook. However, if the events occur in the order before->resolve->after, the ID cannot be obtained in the after event.

I think there are a few solutions.

  1. Give up on deleting the ID information (however, the longer the execution time, the more memory it consumes)

  2. Receive an event when the Promise object is cleaned up. The promise object just before destruction is passed as an argument.

I think nodejs has a function for the latter as a destroy event, but is it possible to implement a similar event hook in quickjs? It would be great if there was a destroy version of a function like the already existing promise_rejection_tracker. It's something like promise_cleanup_tracker.

NOTE: For some reason, promise_rejection_tracker has not been exposed by rquickjs, so I'm not sure if it's a similar function...

@nabetti1720 nabetti1720 changed the title Is it possible to hook into the moment a promise object is destroyed? Is it possible to hook into the moment a promise object is clean up? Apr 27, 2025
@bnoordhuis
Copy link
Contributor

Node's "destroy" hook is really just a GC reclamation callback. You can accomplish the same thing in quickjs with FinalizationRegistry.

@nabetti1720
Copy link
Author

Thank you! I found that it can be used in the JavaScript layer (and also in the LLRT runtime).

The next issue is how to incorporate this into the Rust native implementation, but since this is outside the scope of this issue, I will close it. :)

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

No branches or pull requests

2 participants