Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c109b3c

Browse files
committedDec 30, 2019
n-api: keep napi_env alive while it has finalizers
Manage the napi_env refcount from Finalizer instances, as the finalizer may refer to the napi_env until it is deleted. Fixes: nodejs#31134 Fixes: node-ffi-napi/node-ffi-napi#48
1 parent 8a96d05 commit c109b3c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎src/js_native_api_v8.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ class Finalizer {
195195
_finalize_callback(finalize_callback),
196196
_finalize_data(finalize_data),
197197
_finalize_hint(finalize_hint) {
198+
_env->Ref();
198199
}
199200

200-
~Finalizer() = default;
201+
~Finalizer() {
202+
_env->Unref();
203+
}
201204

202205
public:
203206
static Finalizer* New(napi_env env,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const binding = require(`./build/${common.buildType}/test_buffer`);
5+
const assert = require('assert');
6+
7+
// Regresion test for https://github.com/nodejs/node/issues/31134:
8+
// Buffers with finalizers are allowed to remain alive until
9+
// Environment cleanup without crashing the process.
10+
// The test stores the buffer on `process` to make sure it lives as long as
11+
// the Context does.
12+
13+
process.externalBuffer = binding.newExternalBuffer();
14+
assert.strictEqual(process.externalBuffer.toString(), binding.theText);

0 commit comments

Comments
 (0)
Please sign in to comment.