-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Wrap handle callbacks with console.createTask(...) #47444
Comments
(Leaving this issue mostly as a note to myself to investigate, but others can feel free to look into it on their own and/or comment here on the idea.) |
I'd be happy to have a look. What would be a simple example of code that can benefit from it? |
If we run the following script in the latest Chrome: function makeScheduler() {
const tasks = [];
return {
schedule(f) {
const task = console.createTask(f.name);
tasks.push({ task, f });
},
work() {
while (tasks.length) {
const { task, f } = tasks.shift();
task.run(f); // instead of f();
}
},
};
}
const scheduler = makeScheduler();
scheduler.schedule(function myTask() {
console.log(new Error('foo'));
});
setTimeout(() => {
scheduler.work()
}, 1); The output of the console should be: (notice that the stacktrace only contains where the task is processed)
However, if we set a breakpoint in the
|
My basic thinking was to have a wrapper something like this: function makeCallbackTask(name, callback) {
const task = console.createTask(name);
return function callbackTask(...args) {
return task.run(() => callback.apply(this, args));
};
} Which could be used to replace this: (https://github.com/nodejs/node/blob/main/lib/fs.js#L230-L231) const req = new FSReqCallback();
req.oncomplete = callback; with something like this: const req = new FSReqCallback();
req.oncomplete = makeCallbackTask('FSReq', callback); Basically anywhere that we attach those We would probably want something a bit more performant than making a closure every time, so could be we make a task class or something like that, but this illustrates the general idea. |
I tried it with a simple script: const fs = require('fs');
fs.access(__filename, fs.constants.F_OK, () => {
throw new Error('test')
}); With Node.js 19.8.1:
With your suggestion:
Am I missing something? |
From what @legendecas was saying, it will only do the longer stack trace when using the debugger. Just throwing an error will not produce the longer stack trace. A bit less useful than I was hoping. It would be amazing if it could always produce the longer stack trace. |
I also tried the debugger, and in that case I already got a long stacktrace without even applying the patch (on v19.8.1). |
I think even if we implement |
Ah, could maybe use some clarification then on if there's something we're already doing that is getting us that. Could be it's just only usable for userland then.
Agreed. At the least we should implement such an experiment behind a flag so we can measure perf impact, how successful it actually is at producing these better stack traces, etc. As I tried to convey in my initial messaging, I haven't looked very closely at this yet. It's as much a note to my future self to see if it's even a reasonable idea as it is a feature suggestion. It sounds like there's possibly some value there, but yet to be seen if it's worth whatever tradeoffs would be needed. |
Yeah, async tasks tracked by async hooks are already reported to the inspector: #13870.
Updated: It is exposed already. |
It already is exposed. |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
What is the problem this feature will solve?
Currently stack traces are not great with most callback-based code. Some modules exist to produce long stack traces with
async_hooks
, however the performance impact can be significant. V8 now has its own built-in solution, so we should investigate if that can produce better results.What is the feature you are proposing to solve the problem?
A recent V8 update has provided the
console.createTask(...)
API. We can start pairing that with our existing handle callbacks to gain much better stack traces.See: #44792
It also has the potential to be integrated with the
AsyncContext
spec in the future to be used as an equivalent toAsyncResource
. This could mean we get context-linkage of callback-based code automatically in the future.cc @nodejs/diagnostics
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: