src: close HandleWraps instead of deleting them in OnGCCollect()

When all strong `BaseObjectPtr`s to a `HandleWrap` are gone, we
should not delete the `HandleWrap` outright, but instead close
it and then delete it only once the libuv close callback has been
called.

Based on the valgrind output from the issue below, this has a
good chance of fixing it.

Fixes: https://github.com/nodejs/node/issues/39036

PR-URL: https://github.com/nodejs/node/pull/39441
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen authored and Michaël Zasso committed 2021-09-04 15:14:33 +02:00
1 parent d863a9db68
commit e6ff7e648e
2 files changed
+11 -2

No files matched your search

+1 -1
View File
@@ -207,7 +207,7 @@ void BaseObject::decrease_refcount() {
unsigned int new_refcount = --metadata->strong_ptr_count;
if (new_refcount == 0) {
if (metadata->is_detached) {
delete this;
OnGCCollect();
} else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
MakeWeak();
}
+10 -1
View File
@@ -84,7 +84,16 @@ void HandleWrap::Close(Local<Value> close_callback) {
void HandleWrap::OnGCCollect() {
Close();
// When all references to a HandleWrap are lost and the object is supposed to
// be destroyed, we first call Close() to clean up the underlying libuv
// handle. The OnClose callback then acquires and destroys another reference
// to that object, and when that reference is lost, we perform the default
// action (i.e. destroying `this`).
if (state_ != kClosed) {
Close();
} else {
BaseObject::OnGCCollect();
}
}