-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
N-API feature request: expose API to resolve promises asynchronously without napi_create_async_work #15604
Comments
I spent some time looking at this today and have a first cut at api additions in this branch:https://github.com/mhdawson/io.js/tree/promise_resolve It adds: napi_open_callback_scope And this is some test code that I've been working with:
What I don't quite have yet is a way to test it out properly. @rolftimmermans can you take a look and fill in a bit more info about what would make a good test to add to our addons-napi test suite. Note that I did not incorporate the open/close of a handle scope it open/close callback so if there is not one already in force that would need to be added to the code shown above. |
The other thing I'd want to poke to get a bit more detail at is why napi_create_async_work()/napi_queue_async_work() cannot be used. |
I'll have a look soon to see how this fits in my code. The reason napi_create_async_work()/napi_queue_async_work() is not adequate is that the promise will be resolved by polling on a socket (with libuv and |
Just wondering if scheduling aysnc work, doing the fixed amount of work and then scheduling another async work for the next time has issues ? |
Possibly related, also with suggested API addition #13512 |
Sorry looks like I had the wrong branch in the post above. Looking for the right one now. |
I think I had the write branch may just not have add changes. Will recreate. |
So new branch is here: https://github.com/mhdawson/io.js/tree/callback_scope (top commit). Working towards a PR as @addaleax suggested it would be good to expose this. @rolftimmermans would still like your input as to whether is addresses your use case. It has tests which are the ported versions from the non-N-API addon tests, but test-async-hooks.js is failing for some reason. The correct async id is not passed to the registered hook. From some initial investigation it seems to be ok when the node::CallbackScope object is created inside of napi_open_callback_scope , but is not correct when I'm still investigating but @addaleax if you have any suggestions off the top of your head from the symptoms please let me know. |
This seems to be working fine, thanks! With this change I am able to build the project without including The test suite passes. I'll try to see if I manage to break it if I try hard enough, but for now it's looking good. The C-API wasn't very ergonomic to use, but I imagine that can be solved in the node-addon-api project. |
Btw, I've been passing in |
@rolftimmermans Thanks for he confirmation. I think passing in may be ok, I still need to add the documentation and I'll make sure to check and capture there. Like;y will get to that early in Jan as I still need to figure out the failure in the async-hooks test and I'm off on holiday starting Friday. |
@mhdawson why https://github.com/Globik/libqrencode-js/blob/master/simple_test/test_async.c |
👍 It would be great to see a more flexible way of entering a callback scope get added to the stable API. |
This came up in the TSE meeting today and I'm going to rebase and then open a Work in progress PR mentioning the test that I still don't have working. @addaleax will then help me figure out what the issue is. |
PR here #18089 |
@rolftimmermans, finally got back to this, resolved issues in tests and landed it. |
I must be doing something incorrectly, but I'm hitting a v8 assertion when I attempt to use this feature in Node 9.6 or 9.7 or master. I am calling My program aborts with the following trace:
It seems I'm running into an assertion in V8 on these lines: Lines 72 to 76 in 4c4af64
Unfortunately my knowledge of this change and V8 is too limited to understand why I'm running into this. @mhdawson Perhaps you could give me any pointers to troubleshoot? |
Declaring a |
It works, sort of – except the crash is now moved to the API call that attempts to create a V8 object (in this case It seems Crash is now:
|
You always need to have a Without a handle scope, how would you pass |
Right, OK! Somehow I missed that. I wonder if I misremember how I tested it previously or if something is different between the original PR and how it landed. Anyway. Without any changes to Node whatsoever the code works if I call The precise order of API calls that are required was definitely not straightforward for me, but to make this easier I guess that's why we have nodejs/node-addon-api#223. Thanks! Feel free to ignore this report otherwise.
I seem to remember I had been passing |
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. PR-URL: nodejs#18089 Fixes: nodejs#15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. PR-URL: nodejs#18089 Fixes: nodejs#15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. Backport-PR-URL: #19447 PR-URL: #18089 Fixes: #15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. Backport-PR-URL: #19265 PR-URL: #18089 Fixes: #15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Add support for the following methods; napi_open_callback_scope napi_close_callback_scope These are needed when running asynchronous methods directly using uv. PR-URL: nodejs#18089 Fixes: nodejs#15604 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
My use case: I have a socket that I'm polling with libuv
uv_poll_t
. When an event is present I want to resolve a promise.Calling
napi_resolve_deferred()
does not work for me because it does not appear to run microtasks.Looking at the test added in this commit I need to use the following code before resolving the promise.
This works perfectly; but now I am required to pull in both
v8.h
andnode.h
. It would be nice if there were N-API method to resolve promises withnapi_resolve_deferred()
asynchronously.Note that I am not able to use
napi_create_async_work()
/napi_queue_async_work()
for the async code in this case.The text was updated successfully, but these errors were encountered: