Skip to content
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

Restore an *actual* async resource, not just the ID #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fibers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function setupAsyncHacks(Fiber) {
// Older (or newer?) versions of node may not support this API
try {
var aw = process.binding('async_wrap');
var ah = require('async_hooks');
Copy link
Author

Choose a reason for hiding this comment

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

for some reason my editor hates tabs :(

var getAsyncIdStackSize;

if (aw.asyncIdStackSize instanceof Function) {
Expand Down Expand Up @@ -71,6 +72,7 @@ function setupAsyncHacks(Fiber) {
for (; ii > 0; --ii) {
var asyncId = asyncIds[kExecutionAsyncId];
stack[ii - 1] = {
asyncResource: ah.executionAsyncResource(),
asyncId: asyncId,
triggerId: asyncIds[kTriggerAsyncId],
};
Expand All @@ -82,6 +84,7 @@ function setupAsyncHacks(Fiber) {
function restoreStack(stack) {
for (var ii = 0; ii < stack.length; ++ii) {
pushAsyncContext(stack[ii].asyncId, stack[ii].triggerId);
aw.execution_async_resources.push(stack[ii].asyncResource);
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/async-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if (process.versions.modules < 57) {
console.log('pass');
return;
}
const { AsyncResource } = require('async_hooks');
const { AsyncResource, executionAsyncResource } = require('async_hooks');
const Fiber = require('fibers');

class TestResource extends AsyncResource {
Expand All @@ -28,7 +28,12 @@ class TestResource extends AsyncResource {
let tmp = Fiber(function() {
let resource = new TestResource;
resource.run(function() {
let asyncResource = executionAsyncResource();
Fiber.yield();
if (executionAsyncResource() !== asyncResource) {
console.log('fail');
throw new Error('executionAsyncResource not restored correctly');
}
});
});
tmp.run();
Expand Down