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

async_hooks: use resource stack for AsyncLocalStorage run #39890

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions benchmark/async_hooks/async-local-storage-run.js
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common.js');
const { AsyncLocalStorage } = require('async_hooks');

const bench = common.createBenchmark(main, {
n: [1e7]
});

async function run(store, n) {
for (let i = 0; i < n; i++) {
await new Promise((resolve) => store.run(i, resolve));
}
}

function main({ n }) {
const store = new AsyncLocalStorage();
bench.start();
run(store, n).then(() => {
bench.end(n);
});
}
20 changes: 12 additions & 8 deletions lib/async_hooks.js
Expand Up @@ -263,7 +263,6 @@ const storageHook = createHook({
}
});

const defaultAlsResourceOpts = { requireManualDestroy: true };
class AsyncLocalStorage {
constructor() {
this.kResourceStore = Symbol('kResourceStore');
Expand Down Expand Up @@ -309,14 +308,19 @@ class AsyncLocalStorage {
if (ObjectIs(store, this.getStore())) {
return ReflectApply(callback, null, args);
}
const resource = new AsyncResource('AsyncLocalStorage',
defaultAlsResourceOpts);
// Calling emitDestroy before runInAsyncScope avoids a try/finally
// It is ok because emitDestroy only schedules calling the hook
return resource.emitDestroy().runInAsyncScope(() => {
this.enterWith(store);

this._enable();

const resource = executionAsyncResource();
const oldStore = resource[this.kResourceStore];

resource[this.kResourceStore] = store;

try {
return ReflectApply(callback, null, args);
});
} finally {
resource[this.kResourceStore] = oldStore;
}
}

exit(callback, ...args) {
Expand Down
30 changes: 0 additions & 30 deletions test/async-hooks/test-async-local-storage-run-resource.js

This file was deleted.