From 793282f6a0beacf321ee3cadb517d4458cb28076 Mon Sep 17 00:00:00 2001 From: Stephen Belanger Date: Wed, 18 Dec 2019 21:54:18 -0800 Subject: [PATCH] Give top-level an empty object as a resource --- doc/api/async_hooks.md | 2 -- src/env.cc | 4 ++++ test/parallel/test-async-hooks-current-resource-await.js | 2 ++ test/parallel/test-async-hooks-current-resource.js | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 8340563cb40b22..90656f67c6af15 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -509,8 +509,6 @@ const server = createServer(function(req, res) { }).listen(3000); ``` -`executionAsyncResource()` will return `null` during application bootstrap. - Resource objects returned by `executionAsyncResource()` are often internal handle objects with undocumented APIs. Using any functions or properties on the object is not recommended and may crash your application. diff --git a/src/env.cc b/src/env.cc index c816ea82d6a7d1..7d198c67aa0462 100644 --- a/src/env.cc +++ b/src/env.cc @@ -382,6 +382,8 @@ Environment::Environment(IsolateData* isolate_data, async_hooks_.no_force_checks(); } + async_hooks_.push_execution_async_resource(v8::Object::New(isolate_)); + // TODO(joyeecheung): deserialize when the snapshot covers the environment // properties. CreateProperties(); @@ -418,6 +420,8 @@ Environment::~Environment() { TRACE_EVENT_NESTABLE_ASYNC_END0( TRACING_CATEGORY_NODE1(environment), "Environment", this); + async_hooks_.pop_execution_async_resource(); + // Do not unload addons on the main thread. Some addons need to retain memory // beyond the Environment's lifetime, and unloading them early would break // them; with Worker threads, we have the opportunity to be stricter. diff --git a/test/parallel/test-async-hooks-current-resource-await.js b/test/parallel/test-async-hooks-current-resource-await.js index d850cfdcad7962..9cf65549e6ba00 100644 --- a/test/parallel/test-async-hooks-current-resource-await.js +++ b/test/parallel/test-async-hooks-current-resource-await.js @@ -11,6 +11,8 @@ const id = Symbol('id'); // Tests continuation local storage with the currentResource API // through an async function +assert.ok(executionAsyncResource()); + createHook({ init(asyncId, type, triggerAsyncId, resource) { const cr = executionAsyncResource(); diff --git a/test/parallel/test-async-hooks-current-resource.js b/test/parallel/test-async-hooks-current-resource.js index 41c49c81a8bf6d..d4f1ba93b2792d 100644 --- a/test/parallel/test-async-hooks-current-resource.js +++ b/test/parallel/test-async-hooks-current-resource.js @@ -9,6 +9,8 @@ const id = Symbol('id'); // Tests continuation local storage with the executionAsyncResource API +assert.ok(executionAsyncResource()); + createHook({ init(asyncId, type, triggerAsyncId, resource) { const cr = executionAsyncResource();