Skip to content

Commit 992703f

Browse files
committed
src: prevent persistent handle resource leaks
Replace v8::Persistent with node::Persistent, a specialization that resets the persistent handle on destruction. Prevents accidental resource leaks when forgetting to call .Reset() manually. I'm fairly confident this commit fixes a number of resource leaks that have gone undiagnosed so far. PR-URL: #18656 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent e53275d commit 992703f

25 files changed

+87
-76
lines changed

node.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,10 @@
371371
'src/node_internals.h',
372372
'src/node_javascript.h',
373373
'src/node_mutex.h',
374-
'src/node_platform.h',
375374
'src/node_perf.h',
376375
'src/node_perf_common.h',
376+
'src/node_persistent.h',
377+
'src/node_platform.h',
377378
'src/node_root_certs.h',
378379
'src/node_version.h',
379380
'src/node_watchdog.h',

src/async_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ static void DisablePromiseHook(const FunctionCallbackInfo<Value>& args) {
409409
class DestroyParam {
410410
public:
411411
double asyncId;
412-
v8::Persistent<Object> target;
413-
v8::Persistent<Object> propBag;
412+
Persistent<Object> target;
413+
Persistent<Object> propBag;
414414
};
415415

416416

src/base_object-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ inline BaseObject::~BaseObject() {
4747
}
4848

4949

50-
inline v8::Persistent<v8::Object>& BaseObject::persistent() {
50+
inline Persistent<v8::Object>& BaseObject::persistent() {
5151
return persistent_handle_;
5252
}
5353

src/base_object.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2626

27+
#include "node_persistent.h"
2728
#include "v8.h"
2829

2930
namespace node {
@@ -39,12 +40,7 @@ class BaseObject {
3940
// persistent.IsEmpty() is true.
4041
inline v8::Local<v8::Object> object();
4142

42-
// The parent class is responsible for calling .Reset() on destruction
43-
// when the persistent handle is strong because there is no way for
44-
// BaseObject to know when the handle goes out of scope.
45-
// Weak handles have been reset by the time the destructor runs but
46-
// calling .Reset() again is harmless.
47-
inline v8::Persistent<v8::Object>& persistent();
43+
inline Persistent<v8::Object>& persistent();
4844

4945
inline Environment* env() const;
5046

@@ -71,7 +67,7 @@ class BaseObject {
7167
// position of members in memory are predictable. For more information please
7268
// refer to `doc/guides/node-postmortem-support.md`
7369
friend int GenDebugSymbols();
74-
v8::Persistent<v8::Object> persistent_handle_;
70+
Persistent<v8::Object> persistent_handle_;
7571
Environment* env_;
7672
};
7773

src/env-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ void Environment::CreateImmediate(native_immediate_callback cb,
550550
native_immediate_callbacks_.push_back({
551551
cb,
552552
data,
553-
std::unique_ptr<v8::Persistent<v8::Object>>(obj.IsEmpty() ?
554-
nullptr : new v8::Persistent<v8::Object>(isolate_, obj)),
553+
std::unique_ptr<Persistent<v8::Object>>(obj.IsEmpty() ?
554+
nullptr : new Persistent<v8::Object>(isolate_, obj)),
555555
ref
556556
});
557557
immediate_info()->count_inc(1);

src/env.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ class Environment {
847847
struct NativeImmediateCallback {
848848
native_immediate_callback cb_;
849849
void* data_;
850-
std::unique_ptr<v8::Persistent<v8::Object>> keep_alive_;
850+
std::unique_ptr<Persistent<v8::Object>> keep_alive_;
851851
bool refed_;
852852
};
853853
std::vector<NativeImmediateCallback> native_immediate_callbacks_;
@@ -858,8 +858,7 @@ class Environment {
858858
v8::Local<v8::Promise> promise,
859859
v8::Local<v8::Value> parent);
860860

861-
#define V(PropertyName, TypeName) \
862-
v8::Persistent<TypeName> PropertyName ## _;
861+
#define V(PropertyName, TypeName) Persistent<TypeName> PropertyName ## _;
863862
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
864863
#undef V
865864

src/inspector_agent.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ using v8::HandleScope;
3030
using v8::Isolate;
3131
using v8::Local;
3232
using v8::Object;
33-
using v8::Persistent;
3433
using v8::String;
3534
using v8::Value;
3635

src/inspector_agent.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Agent {
9797

9898
private:
9999
void ToggleAsyncHook(v8::Isolate* isolate,
100-
const v8::Persistent<v8::Function>& fn);
100+
const Persistent<v8::Function>& fn);
101101

102102
node::Environment* parent_env_;
103103
std::unique_ptr<NodeInspectorClient> client_;
@@ -109,8 +109,8 @@ class Agent {
109109

110110
bool pending_enable_async_hook_;
111111
bool pending_disable_async_hook_;
112-
v8::Persistent<v8::Function> enable_async_hook_function_;
113-
v8::Persistent<v8::Function> disable_async_hook_function_;
112+
Persistent<v8::Function> enable_async_hook_function_;
113+
Persistent<v8::Function> disable_async_hook_function_;
114114
};
115115

116116
} // namespace inspector

src/inspector_js_api.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ using v8::Local;
1919
using v8::MaybeLocal;
2020
using v8::NewStringType;
2121
using v8::Object;
22-
using v8::Persistent;
2322
using v8::String;
2423
using v8::Value;
2524

src/module_wrap.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ class ModuleWrap : public BaseObject {
6262
static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
6363

6464

65-
v8::Persistent<v8::Module> module_;
66-
v8::Persistent<v8::String> url_;
65+
Persistent<v8::Module> module_;
66+
Persistent<v8::String> url_;
6767
bool linked_ = false;
68-
std::unordered_map<std::string, v8::Persistent<v8::Promise>> resolve_cache_;
69-
v8::Persistent<v8::Context> context_;
68+
std::unordered_map<std::string, Persistent<v8::Promise>> resolve_cache_;
69+
Persistent<v8::Context> context_;
7070
};
7171

7272
} // namespace loader

0 commit comments

Comments
 (0)