From 3a96ec5347aa9edac71a4032ac96fb5766e7b8c5 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 30 Aug 2024 16:27:28 -0400 Subject: [PATCH] async_hooks: add an InactiveAsyncContextFrame class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gives a class prototype for AsyncContextFrame that contains the required methods, so that when we swap the prototype, ActiveAsyncContextFrame methods are used instead. Previously, the methods were defined in AsyncContextFrame, so swapping the prototype didn't swap those static methods. Also, make the ActiveAsyncContextFrame extend from Map. Fixes: https://github.com/nodejs/node/issues/54503 PR-URL: https://github.com/nodejs/node/pull/54510 Reviewed-By: Rich Trott Reviewed-By: Yagiz Nizipli Reviewed-By: Stephen Belanger Reviewed-By: James M Snell Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Gerhard Stöbich --- lib/internal/async_context_frame.js | 16 +++++++++------- test/parallel/test-async-context-frame.mjs | 4 +++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/internal/async_context_frame.js b/lib/internal/async_context_frame.js index fbf094e113375c..4e76dbac3dd35a 100644 --- a/lib/internal/async_context_frame.js +++ b/lib/internal/async_context_frame.js @@ -11,7 +11,7 @@ const { let enabled_; -class ActiveAsyncContextFrame { +class ActiveAsyncContextFrame extends Map { static get enabled() { return true; } @@ -50,12 +50,7 @@ function checkEnabled() { return enabled; } -class AsyncContextFrame extends Map { - constructor(store, data) { - super(AsyncContextFrame.current()); - this.set(store, data); - } - +class InactiveAsyncContextFrame extends Map { static get enabled() { enabled_ ??= checkEnabled(); return enabled_; @@ -65,6 +60,13 @@ class AsyncContextFrame extends Map { static set(frame) {} static exchange(frame) {} static disable(store) {} +} + +class AsyncContextFrame extends InactiveAsyncContextFrame { + constructor(store, data) { + super(AsyncContextFrame.current()); + this.set(store, data); + } disable(store) { this.delete(store); diff --git a/test/parallel/test-async-context-frame.mjs b/test/parallel/test-async-context-frame.mjs index 6c355b1f09ee5c..cad5d07bbd17cd 100644 --- a/test/parallel/test-async-context-frame.mjs +++ b/test/parallel/test-async-context-frame.mjs @@ -5,6 +5,7 @@ import { opendir } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import { describe, it } from 'node:test'; import { sep } from 'node:path'; +import { strictEqual } from 'node:assert'; const python = process.env.PYTHON || (isWindows ? 'python' : 'python3'); @@ -53,7 +54,8 @@ describe('AsyncContextFrame', { stdio: ['ignore', 'ignore', 'inherit'], }); - await once(proc, 'exit'); + const [code] = await once(proc, 'exit'); + strictEqual(code, 0, `Test ${test} failed with exit code ${code}`); }); } });