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: refactor to avoid unsafe array iteration #37125

Merged
merged 1 commit into from
Feb 1, 2021
Merged
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
15 changes: 7 additions & 8 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class AsyncHook {
// enable()/disable() are run during their execution. The following
// references are reassigned to the tmp arrays if a hook is currently being
// processed.
const [hooks_array, hook_fields] = getHookArrays();
const { 0: hooks_array, 1: hook_fields } = getHookArrays();

// Each hook is only allowed to be added once.
if (ArrayPrototypeIncludes(hooks_array, this))
Expand Down Expand Up @@ -118,7 +118,7 @@ class AsyncHook {
}

disable() {
const [hooks_array, hook_fields] = getHookArrays();
const { 0: hooks_array, 1: hook_fields } = getHookArrays();

const index = ArrayPrototypeIndexOf(hooks_array, this);
if (index === -1)
Expand Down Expand Up @@ -195,8 +195,7 @@ class AsyncResource {
emitBefore(asyncId, this[trigger_async_id_symbol], this);

try {
const ret = thisArg === undefined ?
fn(...args) :
const ret =
ReflectApply(fn, thisArg, args);

return ret;
Expand Down Expand Up @@ -308,25 +307,25 @@ class AsyncLocalStorage {
run(store, callback, ...args) {
// Avoid creation of an AsyncResource if store is already active
if (ObjectIs(store, this.getStore())) {
return callback(...args);
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);
return callback(...args);
return ReflectApply(callback, null, args);
});
}

exit(callback, ...args) {
if (!this.enabled) {
return callback(...args);
return ReflectApply(callback, null, args);
}
this.disable();
try {
return callback(...args);
return ReflectApply(callback, null, args);
} finally {
this._enable();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,14 @@ function clearDefaultTriggerAsyncId() {

function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
if (triggerAsyncId === undefined)
return block(...args);
return ReflectApply(block, null, args);
// CHECK(NumberIsSafeInteger(triggerAsyncId))
// CHECK(triggerAsyncId > 0)
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;

try {
return block(...args);
return ReflectApply(block, null, args);
} finally {
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
}
Expand Down