-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add support for AsyncResource #1472
Add support for AsyncResource #1472
Conversation
I sadly can't quite figure out why this is breaking some tests. A simple Is there like some hidden property bluebird attaches to them? |
Alright, found the specific compare to There's one thing which is kinda "unimplemented" which is |
CC @addaleax |
src/promise.js
Outdated
@@ -418,11 +427,11 @@ Promise.prototype._addCallbacks = function ( | |||
this._receiver0 = receiver; | |||
if (typeof fulfill === "function") { | |||
this._fulfillmentHandler0 = | |||
domain === null ? fulfill : util.domainBind(domain, fulfill); | |||
domain === null ? fulfill : util.contextBind(domain, fulfill); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should drop the domain null checks here, they never do anything now.
return ret; | ||
}; | ||
if (util.nodeSupportsAsyncResource) { | ||
var AsyncResource = require("async_hooks").AsyncResource; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this stripped somehow in the browser build? E.g. will this expression make it into js/browser/bluebird.js
? If so, it might cause problems while bundling in apps using bluebird. Maybe we'd want to put this logic into a completely separate file that's "guarded" by a file-level browser override so it's excluded from bundling? E.g. something like this:
// src/getContext.browser.js, gets loaded directly in browser environments
module.exports = function getContext() { return {}; }
// src/getContext.js, gets loaded in not-definitely-browser environments
var getFallbackContext = require('./getContext.browser.js');
var getContext;
// [...]
// src/promise.js
var getContext = require('./getContext');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, see the exclude
in the browserify instructions, require will just return an empty object in that case (and even that won't be executed due to the isNode check before).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. It compiles to _dereq_('async_hooks')
which just never gets executed. 👍
Another attempt to solicit feedback from @addaleax about this |
@benjamingr Sorry, the reason I haven’t commented so far is: The async_hooks API sucks for these use cases at this point, and we’re going to fix that, but that hasn’t happened yet. Every resource needs a So, uh, I have this on my radar… if anybody wants to make the necessary improvements inside of Node themselves, please go for it, and know that I’m available (same handle on twitter/irc) for any questions, help, guidance, whatever :) |
@addaleax nodejs/node#16153 would be the ticket for that, I assume? @benjamingr Do you want to leave this open until the changes in node land? |
@Sebmaster Yeah, thanks for looking that up. 😄 I’ve labeled that as a good first issue. Also, I guess it doesn’t hurt to leave this open or even merge this without GC support – async_hooks is experimental for a reason, and this is just plainly a bug in core. |
As for the actual diff here, I think there might be too much bluebird-specific stuff in it for me to grok easily? |
In that case we'd need at least a config option to enable AsyncResource, I think (and disable by default). Can't start leaking asyncIds on a random minor bluebird update. |
Thanks for chiming in @addaleax I'd think we'd rather explicitly not support async_hooks than give users something that can leak memory at this point. I'm ok with leaving this open until this gets fixed in core |
New node release is out and has support for automatic destroy call on GC, so limiting async_hooks support to 9.3.0 and up. Should be ready now. |
src/promise.js
Outdated
getContext = function() { | ||
return { | ||
domain: process.domain, | ||
async: new AsyncResource("Promise") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should consider calling it bluebird.Promise
to not confuse it with the builtin PROMISE
.
@addaleax @Sebmaster master do we want to expose an |
Excuse me for cutting in.
|
@Sebmaster @suguru03 This makes sense. @addaleax We should consider only calling |
Alright, fixed all the feedback, added a config option for enabling AsyncResource. @AndreasMadsen Sure we could emit the event, however it seems like looping through the proper AsyncResource is kinda difficult right now, since it's not really promise-instance-specific, but just some param which gets weaved through the API. I think there might be a behaviour difference here now. Native promises generate a single AsyncResource for the whole promise lifecycle(?), while this current implementation creates one AsyncResource per |
Hi @Sebmaster -- any update on this PR? It's been almost a year since it has first opened, so I'm sure some of the concerns about it landing (with regards to the async_hooks API) might have been alleviated in the meantime. On the other hand, if there are still concrete things that still need to be done in Node core, maybe it would be good to mention what those things are now. |
I'd love this PR to land in bluebird as well |
Just had to completely toss out Bluebird from our codebase and replace it with native Promises (combined with https://github.com/asfktz/Awaity.js) due to Bluebird not playing nice with other libraries that are based on async_hooks |
Previous versions of node did not emit a destroy event on gc, leading to leaked async ids.
node 0.10 doesn't like it
So I just restarted work on this to finally get this in. Test suite passes, I think the performance issues have been fixed in core (was not able to reproduce the 10x doxbee bench slowdown with v10.9.0 with hooks in Bluebird enabled - I think because no hooks are listening) and I think the native vs bluebird Promise behaviour should not be an issue; it's acceptable with regards to the async_hooks spec. @benjamingr Can I get some concrete maintainer guidance on what you'd like to see to merge this? |
@Sebmaster this would need a review from someone who is very familiar with AsyncResource and someone who is a maintainer. I can do the bluebird code part and maybe @addaleax or @AndreasMadsen has time to take a look at the AsyncResource stuff? Then I need to ping @petkaantonov IRL to do a release :D |
continues here #1597 |
I kinda hijacked the domain architecture to implement AsyncResource, but it makes the changes very consistent and keeps the context stuff transparent to the rest of the code.
Fixes #1403.