src: use cleanup hooks to tear down BaseObjects · nodejs/node@9e2554c · GitHub
Skip to content

Commit 9e2554c

Browse files
committed
src: use cleanup hooks to tear down BaseObjects
Clean up after `BaseObject` instances when the `Environment` is being shut down. This takes care of closing non-libuv resources like `zlib` instances, which do not require asynchronous shutdown. Many thanks for Stephen Belanger, Timothy Gu and Alexey Orlenko for reviewing the original version of this commit in the Ayo.js project. Refs: ayojs/ayo#88 PR-URL: #19377 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8995408 commit 9e2554c

6 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/base_object-inl.h

Lines changed: 9 additions & 0 deletions

src/base_object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class BaseObject {
7171
private:
7272
BaseObject();
7373

74+
static inline void DeleteMe(void* data);
75+
7476
// persistent_handle_ needs to be at a fixed offset from the start of the
7577
// class because it is used by src/node_postmortem_metadata.cc to calculate
7678
// offsets and generate debug symbols for BaseObject, which assumes that the

src/env.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ Environment::Environment(IsolateData* isolate_data,
133133
}
134134

135135
Environment::~Environment() {
136+
// Make sure there are no re-used libuv wrapper objects.
137+
// CleanupHandles() should have removed all of them.
138+
CHECK(file_handle_read_wrap_freelist_.empty());
139+
136140
v8::HandleScope handle_scope(isolate());
137141

138142
#if HAVE_INSPECTOR
@@ -245,6 +249,8 @@ void Environment::CleanupHandles() {
245249
!handle_wrap_queue_.IsEmpty()) {
246250
uv_run(event_loop(), UV_RUN_ONCE);
247251
}
252+
253+
file_handle_read_wrap_freelist_.clear();
248254
}
249255

250256
void Environment::StartProfilerIdleNotifier() {

src/inspector_agent.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ std::unique_ptr<InspectorSession> Agent::Connect(
576576

577577
void Agent::WaitForDisconnect() {
578578
CHECK_NE(client_, nullptr);
579+
// TODO(addaleax): Maybe this should use an at-exit hook for the Environment
580+
// or something similar?
579581
client_->contextDestroyed(parent_env_->context());
580582
if (io_ != nullptr) {
581583
io_->WaitForDisconnect();

src/node.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4578,12 +4578,13 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
45784578

45794579
const int exit_code = EmitExit(&env);
45804580

4581+
WaitForInspectorDisconnect(&env);
4582+
45814583
env.RunCleanup();
45824584
RunAtExit(&env);
45834585

45844586
v8_platform.DrainVMTasks(isolate);
45854587
v8_platform.CancelVMTasks(isolate);
4586-
WaitForInspectorDisconnect(&env);
45874588
#if defined(LEAK_SANITIZER)
45884589
__lsan_do_leak_check();
45894590
#endif

src/req_wrap-inl.h

Lines changed: 0 additions & 1 deletion

0 commit comments

Comments
 (0)