GeezerVison is showing github.com — async_wrap: call callback in destructor · nodejs/node@b663d2b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/async-wrap-inl.h +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,11 +41,12 @@ inline AsyncWrap::AsyncWrap(Environment* env, | 41 | 41 |
| 42 | 42 | v8::Local<v8::Value> argv[] = { | 43 | 43 | v8::Int32::New(env->isolate(), provider), | | 44 | + v8::Integer::New(env->isolate(), get_uid()), | 44 | 45 | Null(env->isolate()) | 45 | 46 | }; | 46 | 47 |
| 47 | 48 | if (parent != nullptr) | 48 | | - argv[1] = parent->object(); | | 49 | + argv[2] = parent->object(); | 49 | 50 |
| 50 | 51 | v8::MaybeLocal<v8::Value> ret = | 51 | 52 | init_fn->Call(env->context(), object, ARRAY_SIZE(argv), argv); | @@ -57,6 +58,22 @@ inline AsyncWrap::AsyncWrap(Environment* env, | 57 | 58 | } | 58 | 59 |
| 59 | 60 |
| | 61 | +inline AsyncWrap::~AsyncWrap() { | | 62 | + if (!ran_init_callback()) | | 63 | + return; | | 64 | + | | 65 | + v8::Local<v8::Function> fn = env()->async_hooks_destroy_function(); | | 66 | + if (!fn.IsEmpty()) { | | 67 | + v8::HandleScope scope(env()->isolate()); | | 68 | + v8::Local<v8::Value> uid = v8::Integer::New(env()->isolate(), get_uid()); | | 69 | + v8::MaybeLocal<v8::Value> ret = | | 70 | + fn->Call(env()->context(), v8::Null(env()->isolate()), 1, &uid); | | 71 | + if (ret.IsEmpty()) | | 72 | + FatalError("node::AsyncWrap::~AsyncWrap", "destroy hook threw"); | | 73 | + } | | 74 | +} | | 75 | + | | 76 | + | 60 | 77 | inline bool AsyncWrap::ran_init_callback() const { | 61 | 78 | return static_cast<bool>(bits_ & 1); | 62 | 79 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/async-wrap.cc +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -131,6 +131,8 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) { | 131 | 131 | env->set_async_hooks_pre_function(args[1].As<Function>()); | 132 | 132 | if (args[2]->IsFunction()) | 133 | 133 | env->set_async_hooks_post_function(args[2].As<Function>()); | | 134 | + if (args[3]->IsFunction()) | | 135 | + env->set_async_hooks_destroy_function(args[3].As<Function>()); | 134 | 136 | } | 135 | 137 |
| 136 | 138 |
| @@ -156,6 +158,7 @@ static void Initialize(Local<Object> target, | 156 | 158 | env->set_async_hooks_init_function(Local<Function>()); | 157 | 159 | env->set_async_hooks_pre_function(Local<Function>()); | 158 | 160 | env->set_async_hooks_post_function(Local<Function>()); | | 161 | + env->set_async_hooks_destroy_function(Local<Function>()); | 159 | 162 | } | 160 | 163 |
| 161 | 164 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/async-wrap.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,7 +51,7 @@ class AsyncWrap : public BaseObject { | 51 | 51 | ProviderType provider, | 52 | 52 | AsyncWrap* parent = nullptr); | 53 | 53 |
| 54 | | - inline virtual ~AsyncWrap() override = default; | | 54 | + inline virtual ~AsyncWrap(); | 55 | 55 |
| 56 | 56 | inline ProviderType provider_type() const; | 57 | 57 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -236,6 +236,7 @@ namespace node { | 236 | 236 | V(async_hooks_init_function, v8::Function) \ | 237 | 237 | V(async_hooks_pre_function, v8::Function) \ | 238 | 238 | V(async_hooks_post_function, v8::Function) \ | | 239 | + V(async_hooks_destroy_function, v8::Function) \ | 239 | 240 | V(binding_cache_object, v8::Object) \ | 240 | 241 | V(buffer_constructor_function, v8::Function) \ | 241 | 242 | V(buffer_prototype_object, v8::Object) \ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-disabled-propagate-parent.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,7 @@ let cntr = 0; | 10 | 10 | let server; | 11 | 11 | let client; | 12 | 12 |
| 13 | | -function init(type, parent) { | | 13 | +function init(type, id, parent) { | 14 | 14 | if (parent) { | 15 | 15 | cntr++; | 16 | 16 | // Cannot assert in init callback or will abort. | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-propagate-parent.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,7 +9,7 @@ let cntr = 0; | 9 | 9 | let server; | 10 | 10 | let client; | 11 | 11 |
| 12 | | -function init(type, parent) { | | 12 | +function init(type, id, parent) { | 13 | 13 | if (parent) { | 14 | 14 | cntr++; | 15 | 15 | // Cannot assert in init callback or will abort. | |
|