Skip to content

Commit 2503259

Browse files
bnoordhuisMylesBorins
authored andcommitted
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. Backport-PR-URL: #19185 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 01348c8 commit 2503259

25 files changed

+97
-82
lines changed

node.gyp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,10 @@
359359
'src/node_internals.h',
360360
'src/node_javascript.h',
361361
'src/node_mutex.h',
362-
'src/node_platform.h',
363362
'src/node_perf.h',
364363
'src/node_perf_common.h',
364+
'src/node_persistent.h',
365+
'src/node_platform.h',
365366
'src/node_root_certs.h',
366367
'src/node_version.h',
367368
'src/node_watchdog.h',

src/async_wrap.cc

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

417417

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
@@ -541,8 +541,8 @@ void Environment::CreateImmediate(native_immediate_callback cb,
541541
native_immediate_callbacks_.push_back({
542542
cb,
543543
data,
544-
std::unique_ptr<v8::Persistent<v8::Object>>(obj.IsEmpty() ?
545-
nullptr : new v8::Persistent<v8::Object>(isolate_, obj)),
544+
std::unique_ptr<Persistent<v8::Object>>(obj.IsEmpty() ?
545+
nullptr : new Persistent<v8::Object>(isolate_, obj)),
546546
ref
547547
});
548548
immediate_info()->count_inc(1);

src/env.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ class Environment {
800800
struct NativeImmediateCallback {
801801
native_immediate_callback cb_;
802802
void* data_;
803-
std::unique_ptr<v8::Persistent<v8::Object>> keep_alive_;
803+
std::unique_ptr<Persistent<v8::Object>> keep_alive_;
804804
bool refed_;
805805
};
806806
std::vector<NativeImmediateCallback> native_immediate_callbacks_;
@@ -811,8 +811,7 @@ class Environment {
811811
v8::Local<v8::Promise> promise,
812812
v8::Local<v8::Value> parent);
813813

814-
#define V(PropertyName, TypeName) \
815-
v8::Persistent<TypeName> PropertyName ## _;
814+
#define V(PropertyName, TypeName) Persistent<TypeName> PropertyName ## _;
816815
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
817816
#undef V
818817

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
@@ -49,11 +49,11 @@ class ModuleWrap : public BaseObject {
4949
v8::Local<v8::String> specifier,
5050
v8::Local<v8::Module> referrer);
5151

52-
v8::Persistent<v8::Module> module_;
53-
v8::Persistent<v8::String> url_;
52+
Persistent<v8::Module> module_;
53+
Persistent<v8::String> url_;
5454
bool linked_ = false;
55-
std::unordered_map<std::string, v8::Persistent<v8::Promise>> resolve_cache_;
56-
v8::Persistent<v8::Context> context_;
55+
std::unordered_map<std::string, Persistent<v8::Promise>> resolve_cache_;
56+
Persistent<v8::Context> context_;
5757
};
5858

5959
} // namespace loader

0 commit comments

Comments
 (0)