Skip to content

Commit e9659f5

Browse files
addaleaxaduh95
authored andcommitted
src: add cleanup hooks to node::ObjectWrap
Add cleanup hooks to make sure that addons making use of this legacy API can do so safely in Worker threads or embedder-controlled Node.js instances. Refs: #63575 Fixes: #63540 Signed-off-by: Anna Henningsen <[email protected]> PR-URL: #63642 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 48a766c commit e9659f5

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/node_object_wrap.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
#ifndef SRC_NODE_OBJECT_WRAP_H_
2323
#define SRC_NODE_OBJECT_WRAP_H_
2424

25-
#include "v8.h"
2625
#include <cassert>
27-
26+
#include "node.h"
2827

2928
namespace node {
3029

3130
class ObjectWrap {
3231
public:
3332
ObjectWrap() {
3433
refs_ = 0;
34+
AddCleanupHook();
3535
}
3636

3737

3838
virtual ~ObjectWrap() {
39+
RemoveCleanupHook();
3940
if (persistent().IsEmpty())
4041
return;
4142
persistent().ClearWeak();
@@ -123,6 +124,16 @@ class ObjectWrap {
123124
delete wrap;
124125
}
125126

127+
void AddCleanupHook() {
128+
AddEnvironmentCleanupHook(v8::Isolate::GetCurrent(), CleanupHook, this);
129+
}
130+
131+
void RemoveCleanupHook() {
132+
RemoveEnvironmentCleanupHook(v8::Isolate::GetCurrent(), CleanupHook, this);
133+
}
134+
135+
static void CleanupHook(void* arg) { delete static_cast<ObjectWrap*>(arg); }
136+
126137
// NOLINTNEXTLINE(runtime/v8_persistent)
127138
v8::Persistent<v8::Object> handle_;
128139
};

0 commit comments

Comments
 (0)