Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: keep napi_env alive while it has finalizers #31140

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ class Finalizer {
_finalize_callback(finalize_callback),
_finalize_data(finalize_data),
_finalize_hint(finalize_hint) {
_env->Ref();
}

~Finalizer() = default;
~Finalizer() {
_env->Unref();
}

public:
static Finalizer* New(napi_env env,
Expand Down
14 changes: 14 additions & 0 deletions test/node-api/test_buffer/test-external-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const common = require('../../common');
const binding = require(`./build/${common.buildType}/test_buffer`);
const assert = require('assert');

// Regresion test for https://github.com/nodejs/node/issues/31134:
addaleax marked this conversation as resolved.
Show resolved Hide resolved
// Buffers with finalizers are allowed to remain alive until
// Environment cleanup without crashing the process.
// The test stores the buffer on `process` to make sure it lives as long as
// the Context does.

process.externalBuffer = binding.newExternalBuffer();
assert.strictEqual(process.externalBuffer.toString(), binding.theText);