GeezerVison is showing github.com — async_wrap: schedule destroy hook as unref · nodejs/node@5ddef29
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.cc +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -159,6 +159,12 @@ static void DestroyAsyncIdsCallback(Environment* env, void* data) { | 159 | 159 | } while (!env->destroy_async_id_list()->empty()); | 160 | 160 | } | 161 | 161 |
| | 162 | +static void DestroyAsyncIdsCallback(void* arg) { | | 163 | + Environment* env = static_cast<Environment*>(arg); | | 164 | + if (!env->destroy_async_id_list()->empty()) | | 165 | + DestroyAsyncIdsCallback(env, nullptr); | | 166 | +} | | 167 | + | 162 | 168 |
| 163 | 169 | void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) { | 164 | 170 | AsyncHooks* async_hooks = env->async_hooks(); | @@ -501,6 +507,8 @@ void AsyncWrap::Initialize(Local<Object> target, | 501 | 507 | Isolate* isolate = env->isolate(); | 502 | 508 | HandleScope scope(isolate); | 503 | 509 |
| | 510 | + env->BeforeExit(DestroyAsyncIdsCallback, env); | | 511 | + | 504 | 512 | env->SetMethod(target, "setupHooks", SetupHooks); | 505 | 513 | env->SetMethod(target, "pushAsyncIds", PushAsyncIds); | 506 | 514 | env->SetMethod(target, "popAsyncIds", PopAsyncIds); | @@ -662,7 +670,7 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) { | 662 | 670 | return; | 663 | 671 |
| 664 | 672 | if (env->destroy_async_id_list()->empty()) { | 665 | | - env->SetImmediate(DestroyAsyncIdsCallback, nullptr); | | 673 | + env->SetUnrefImmediate(DestroyAsyncIdsCallback, nullptr); | 666 | 674 | } | 667 | 675 |
| 668 | 676 | env->destroy_async_id_list()->push_back(async_id); | |
Collapse file Copy file name to clipboardExpand all lines: src/env.cc +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -209,6 +209,17 @@ void Environment::PrintSyncTrace() const { | 209 | 209 | fflush(stderr); | 210 | 210 | } | 211 | 211 |
| | 212 | +void Environment::RunBeforeExitCallbacks() { | | 213 | + for (BeforeExitCallback before_exit : before_exit_functions_) { | | 214 | + before_exit.cb_(before_exit.arg_); | | 215 | + } | | 216 | + before_exit_functions_.clear(); | | 217 | +} | | 218 | + | | 219 | +void Environment::BeforeExit(void (*cb)(void* arg), void* arg) { | | 220 | + before_exit_functions_.push_back(BeforeExitCallback{cb, arg}); | | 221 | +} | | 222 | + | 212 | 223 | void Environment::RunAtExitCallbacks() { | 213 | 224 | for (AtExitCallback at_exit : at_exit_functions_) { | 214 | 225 | at_exit.cb_(at_exit.arg_); | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -644,6 +644,8 @@ class Environment { | 644 | 644 | const char* name, | 645 | 645 | v8::FunctionCallback callback); | 646 | 646 |
| | 647 | + void BeforeExit(void (*cb)(void* arg), void* arg); | | 648 | + void RunBeforeExitCallbacks(); | 647 | 649 | void AtExit(void (*cb)(void* arg), void* arg); | 648 | 650 | void RunAtExitCallbacks(); | 649 | 651 |
| @@ -770,6 +772,12 @@ class Environment { | 770 | 772 |
| 771 | 773 | double* fs_stats_field_array_; | 772 | 774 |
| | 775 | + struct BeforeExitCallback { | | 776 | + void (*cb_)(void* arg); | | 777 | + void* arg_; | | 778 | + }; | | 779 | + std::list<BeforeExitCallback> before_exit_functions_; | | 780 | + | 773 | 781 | struct AtExitCallback { | 774 | 782 | void (*cb_)(void* arg); | 775 | 783 | void* arg_; | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4555,6 +4555,14 @@ void AtExit(Environment* env, void (*cb)(void* arg), void* arg) { | 4555 | 4555 | } | 4556 | 4556 |
| 4557 | 4557 |
| | 4558 | +void RunBeforeExit(Environment* env) { | | 4559 | + env->RunBeforeExitCallbacks(); | | 4560 | + | | 4561 | + if (!uv_loop_alive(env->event_loop())) | | 4562 | + EmitBeforeExit(env); | | 4563 | +} | | 4564 | + | | 4565 | + | 4558 | 4566 | void EmitBeforeExit(Environment* env) { | 4559 | 4567 | HandleScope handle_scope(env->isolate()); | 4560 | 4568 | Context::Scope context_scope(env->context()); | @@ -4705,7 +4713,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data, | 4705 | 4713 | if (more) | 4706 | 4714 | continue; | 4707 | 4715 |
| 4708 | | - EmitBeforeExit(&env); | | 4716 | + RunBeforeExit(&env); | 4709 | 4717 |
| 4710 | 4718 | // Emit `beforeExit` if the loop became alive either after emitting | 4711 | 4719 | // event, or after running some callbacks. | |
|