Skip to content

Commit d38dc99

Browse files
src: add Cleanable class to Environment
We store a linked list of `Cleanable` objects on the `node::Environment` and invoke their `Clean()` method during env teardown. This eliminates the need for adding many cleanup hooks. PR-URL: #54880 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 99bbf80 commit d38dc99

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/env.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,11 @@ void Environment::RunCleanup() {
12791279
// Defer the BaseObject cleanup after handles are cleaned up.
12801280
CleanupHandles();
12811281

1282+
while (!cleanable_queue_.IsEmpty()) {
1283+
Cleanable* cleanable = cleanable_queue_.PopFront();
1284+
cleanable->Clean();
1285+
}
1286+
12821287
while (!cleanup_queue_.empty() || principal_realm_->HasCleanupHooks() ||
12831288
native_immediates_.size() > 0 ||
12841289
native_immediates_threadsafe_.size() > 0 ||

src/env.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,18 @@ void DefaultProcessExitHandlerInternal(Environment* env, ExitCode exit_code);
594594
v8::Maybe<ExitCode> SpinEventLoopInternal(Environment* env);
595595
v8::Maybe<ExitCode> EmitProcessExitInternal(Environment* env);
596596

597+
class Cleanable {
598+
public:
599+
virtual ~Cleanable() = default;
600+
601+
protected:
602+
ListNode<Cleanable> cleanable_queue_;
603+
604+
private:
605+
virtual void Clean() = 0;
606+
friend class Environment;
607+
};
608+
597609
/**
598610
* Environment is a per-isolate data structure that represents an execution
599611
* environment. Each environment has a principal realm. An environment can
@@ -902,8 +914,12 @@ class Environment final : public MemoryRetainer {
902914

903915
typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
904916
typedef ListHead<ReqWrapBase, &ReqWrapBase::req_wrap_queue_> ReqWrapQueue;
917+
typedef ListHead<Cleanable, &Cleanable::cleanable_queue_> CleanableQueue;
905918

906919
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
920+
inline CleanableQueue* cleanable_queue() {
921+
return &cleanable_queue_;
922+
}
907923
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
908924

909925
// https://w3c.github.io/hr-time/#dfn-time-origin
@@ -1192,6 +1208,7 @@ class Environment final : public MemoryRetainer {
11921208
// memory are predictable. For more information please refer to
11931209
// `doc/contributing/node-postmortem-support.md`
11941210
friend int GenDebugSymbols();
1211+
CleanableQueue cleanable_queue_;
11951212
HandleWrapQueue handle_wrap_queue_;
11961213
ReqWrapQueue req_wrap_queue_;
11971214
std::list<HandleCleanup> handle_cleanup_queue_;

src/node_buffer.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ using v8::Value;
8484

8585
namespace {
8686

87-
class CallbackInfo {
87+
class CallbackInfo : public Cleanable {
8888
public:
8989
static inline Local<ArrayBuffer> CreateTrackedArrayBuffer(
9090
Environment* env,
@@ -97,7 +97,7 @@ class CallbackInfo {
9797
CallbackInfo& operator=(const CallbackInfo&) = delete;
9898

9999
private:
100-
static void CleanupHook(void* data);
100+
void Clean();
101101
inline void OnBackingStoreFree();
102102
inline void CallAndResetCallback();
103103
inline CallbackInfo(Environment* env,
@@ -112,7 +112,6 @@ class CallbackInfo {
112112
Environment* const env_;
113113
};
114114

115-
116115
Local<ArrayBuffer> CallbackInfo::CreateTrackedArrayBuffer(
117116
Environment* env,
118117
char* data,
@@ -152,25 +151,23 @@ CallbackInfo::CallbackInfo(Environment* env,
152151
data_(data),
153152
hint_(hint),
154153
env_(env) {
155-
env->AddCleanupHook(CleanupHook, this);
154+
env->cleanable_queue()->PushFront(this);
156155
env->isolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
157156
}
158157

159-
void CallbackInfo::CleanupHook(void* data) {
160-
CallbackInfo* self = static_cast<CallbackInfo*>(data);
161-
158+
void CallbackInfo::Clean() {
162159
{
163-
HandleScope handle_scope(self->env_->isolate());
164-
Local<ArrayBuffer> ab = self->persistent_.Get(self->env_->isolate());
160+
HandleScope handle_scope(env_->isolate());
161+
Local<ArrayBuffer> ab = persistent_.Get(env_->isolate());
165162
if (!ab.IsEmpty() && ab->IsDetachable()) {
166163
ab->Detach(Local<Value>()).Check();
167-
self->persistent_.Reset();
164+
persistent_.Reset();
168165
}
169166
}
170167

171168
// Call the callback in this case, but don't delete `this` yet because the
172169
// BackingStore deleter callback will do so later.
173-
self->CallAndResetCallback();
170+
CallAndResetCallback();
174171
}
175172

176173
void CallbackInfo::CallAndResetCallback() {
@@ -182,7 +179,7 @@ void CallbackInfo::CallAndResetCallback() {
182179
}
183180
if (callback != nullptr) {
184181
// Clean up all Environment-related state and run the callback.
185-
env_->RemoveCleanupHook(CleanupHook, this);
182+
cleanable_queue_.Remove();
186183
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
187184
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
188185

0 commit comments

Comments
 (0)