Skip to content

Commit 98f7d8e

Browse files
Gabriel SchulhofBethGriggs
Gabriel Schulhof
authored andcommitted
n-api: handle weak no-finalizer refs correctly
When deleting a weak reference that has no finalizer we must not defer deletion until the non-existent finalizer gets called. Fixes: #34731 Signed-off-by: Gabriel Schulhof <[email protected]> PR-URL: #34839 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 68b7a8d commit 98f7d8e

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/js_native_api_v8.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ class RefBase : protected Finalizer, RefTracker {
228228
// from one of Unwrap or napi_delete_reference.
229229
//
230230
// When it is called from Unwrap or napi_delete_reference we only
231-
// want to do the delete if the finalizer has already run or
232-
// cannot have been queued to run (ie the reference count is > 0),
231+
// want to do the delete if there is no finalizer or the finalizer has already
232+
// run or cannot have been queued to run (i.e. the reference count is > 0),
233233
// otherwise we may crash when the finalizer does run.
234+
//
234235
// If the finalizer may have been queued and has not already run
235236
// delay the delete until the finalizer runs by not doing the delete
236237
// and setting _delete_self to true so that the finalizer will
@@ -242,6 +243,7 @@ class RefBase : protected Finalizer, RefTracker {
242243
static inline void Delete(RefBase* reference) {
243244
reference->Unlink();
244245
if ((reference->RefCount() != 0) ||
246+
(reference->_finalize_callback == nullptr) ||
245247
(reference->_delete_self) ||
246248
(reference->_finalize_ran)) {
247249
delete reference;

test/node-api/test_worker_terminate_finalization/test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
'use strict';
22
const common = require('../../common');
33

4-
// TODO(addaleax): Run this test once it stops failing under ASAN/valgrind.
5-
// Refs: https://github.com/nodejs/node/issues/34731
6-
common.skip('Reference management in N-API leaks memory');
7-
84
const { Worker, isMainThread } = require('worker_threads');
95

106
if (isMainThread) {

0 commit comments

Comments
 (0)