GeezerVison is showing github.com — n-api: run all finalizers via SetImmediate() · styfle/node@a74a6e3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/napi/ref/index.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,8 +10,10 @@ function callNewWeak() { | 10 | 10 | function main({ n }) { | 11 | 11 | addon.count = 0; | 12 | 12 | bench.start(); | 13 | | - while (addon.count < n) { | 14 | | - callNewWeak(); | 15 | | - } | 16 | | - bench.end(n); | | 13 | + new Promise((resolve) => { | | 14 | + (function oneIteration() { | | 15 | + callNewWeak(); | | 16 | + setImmediate(() => ((addon.count < n) ? oneIteration() : resolve())); | | 17 | + })(); | | 18 | + }).then(() => bench.end(n)); | 17 | 19 | } |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -267,13 +267,7 @@ class RefBase : protected Finalizer, RefTracker { | 267 | 267 | protected: | 268 | 268 | inline void Finalize(bool is_env_teardown = false) override { | 269 | 269 | if (_finalize_callback != nullptr) { | 270 | | - v8::HandleScope handle_scope(_env->isolate); | 271 | | - _env->CallIntoModule([&](napi_env env) { | 272 | | - _finalize_callback( | 273 | | - env, | 274 | | - _finalize_data, | 275 | | - _finalize_hint); | 276 | | - }); | | 270 | + _env->CallFinalizer(_finalize_callback, _finalize_data, _finalize_hint); | 277 | 271 | } | 278 | 272 |
| 279 | 273 | // this is safe because if a request to delete the reference | |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api_v8.h +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,6 +101,13 @@ struct napi_env__ { | 101 | 101 | } | 102 | 102 | } | 103 | 103 |
| | 104 | + virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) { | | 105 | + v8::HandleScope handle_scope(isolate); | | 106 | + CallIntoModule([&](napi_env env) { | | 107 | + cb(env, data, hint); | | 108 | + }); | | 109 | + } | | 110 | + | 104 | 111 | v8impl::Persistent<v8::Value> last_exception; | 105 | 112 |
| 106 | 113 | // We store references in two different lists, depending on whether they have | |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.cc +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,17 @@ struct node_napi_env__ : public napi_env__ { | 32 | 32 | node_env()->untransferable_object_private_symbol(), | 33 | 33 | v8::True(isolate)); | 34 | 34 | } | | 35 | + | | 36 | + void CallFinalizer(napi_finalize cb, void* data, void* hint) override { | | 37 | + napi_env env = static_cast<napi_env>(this); | | 38 | + node_env()->SetImmediate([=](node::Environment* node_env) { | | 39 | + v8::HandleScope handle_scope(env->isolate); | | 40 | + v8::Context::Scope context_scope(env->context()); | | 41 | + env->CallIntoModule([&](napi_env env) { | | 42 | + cb(env, data, hint); | | 43 | + }); | | 44 | + }); | | 45 | + } | 35 | 46 | }; | 36 | 47 |
| 37 | 48 | typedef node_napi_env__* node_napi_env; | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -664,6 +664,30 @@ function skipIfDumbTerminal() { | 664 | 664 | } | 665 | 665 | } | 666 | 666 |
| | 667 | +function gcUntil(name, condition) { | | 668 | + if (typeof name === 'function') { | | 669 | + condition = name; | | 670 | + name = undefined; | | 671 | + } | | 672 | + return new Promise((resolve, reject) => { | | 673 | + let count = 0; | | 674 | + function gcAndCheck() { | | 675 | + setImmediate(() => { | | 676 | + count++; | | 677 | + global.gc(); | | 678 | + if (condition()) { | | 679 | + resolve(); | | 680 | + } else if (count < 10) { | | 681 | + gcAndCheck(); | | 682 | + } else { | | 683 | + reject(name === undefined ? undefined : 'Test ' + name + ' failed'); | | 684 | + } | | 685 | + }); | | 686 | + } | | 687 | + gcAndCheck(); | | 688 | + }); | | 689 | +} | | 690 | + | 667 | 691 | const common = { | 668 | 692 | allowGlobals, | 669 | 693 | buildType, | @@ -673,6 +697,7 @@ const common = { | 673 | 697 | disableCrashOnUnhandledRejection, | 674 | 698 | expectsError, | 675 | 699 | expectWarning, | | 700 | + gcUntil, | 676 | 701 | getArrayBufferViews, | 677 | 702 | getBufferSources, | 678 | 703 | getCallSite, | |
Collapse file Copy file name to clipboardExpand all lines: test/js-native-api/7_factory_wrap/test.js +17-16Lines changed: 17 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,20 +6,21 @@ const assert = require('assert'); | 6 | 6 | const test = require(`./build/${common.buildType}/binding`); | 7 | 7 |
| 8 | 8 | assert.strictEqual(test.finalizeCount, 0); | 9 | | -(() => { | 10 | | - const obj = test.createObject(10); | 11 | | - assert.strictEqual(obj.plusOne(), 11); | 12 | | - assert.strictEqual(obj.plusOne(), 12); | 13 | | - assert.strictEqual(obj.plusOne(), 13); | 14 | | -})(); | 15 | | -global.gc(); | 16 | | -assert.strictEqual(test.finalizeCount, 1); | | 9 | +async function runGCTests() { | | 10 | + (() => { | | 11 | + const obj = test.createObject(10); | | 12 | + assert.strictEqual(obj.plusOne(), 11); | | 13 | + assert.strictEqual(obj.plusOne(), 12); | | 14 | + assert.strictEqual(obj.plusOne(), 13); | | 15 | + })(); | | 16 | + await common.gcUntil('test 1', () => (test.finalizeCount === 1)); | 17 | 17 |
| 18 | | -(() => { | 19 | | - const obj2 = test.createObject(20); | 20 | | - assert.strictEqual(obj2.plusOne(), 21); | 21 | | - assert.strictEqual(obj2.plusOne(), 22); | 22 | | - assert.strictEqual(obj2.plusOne(), 23); | 23 | | -})(); | 24 | | -global.gc(); | 25 | | -assert.strictEqual(test.finalizeCount, 2); | | 18 | + (() => { | | 19 | + const obj2 = test.createObject(20); | | 20 | + assert.strictEqual(obj2.plusOne(), 21); | | 21 | + assert.strictEqual(obj2.plusOne(), 22); | | 22 | + assert.strictEqual(obj2.plusOne(), 23); | | 23 | + })(); | | 24 | + await common.gcUntil('test 2', () => (test.finalizeCount === 2)); | | 25 | +} | | 26 | +runGCTests(); |
Collapse file Copy file name to clipboardExpand all lines: test/js-native-api/8_passing_wrapped/test.js +12-9Lines changed: 12 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,13 +5,16 @@ const common = require('../../common'); | 5 | 5 | const assert = require('assert'); | 6 | 6 | const addon = require(`./build/${common.buildType}/binding`); | 7 | 7 |
| 8 | | -let obj1 = addon.createObject(10); | 9 | | -let obj2 = addon.createObject(20); | 10 | | -const result = addon.add(obj1, obj2); | 11 | | -assert.strictEqual(result, 30); | | 8 | +async function runTest() { | | 9 | + let obj1 = addon.createObject(10); | | 10 | + let obj2 = addon.createObject(20); | | 11 | + const result = addon.add(obj1, obj2); | | 12 | + assert.strictEqual(result, 30); | 12 | 13 |
| 13 | | -// Make sure the native destructor gets called. | 14 | | -obj1 = null; | 15 | | -obj2 = null; | 16 | | -global.gc(); | 17 | | -assert.strictEqual(addon.finalizeCount(), 2); | | 14 | + // Make sure the native destructor gets called. | | 15 | + obj1 = null; | | 16 | + obj2 = null; | | 17 | + await common.gcUntil('8_passing_wrapped', | | 18 | + () => (addon.finalizeCount() === 2)); | | 19 | +} | | 20 | +runTest(); |
Collapse file Copy file name to clipboardExpand all lines: test/js-native-api/test_exception/test.js -11Lines changed: 0 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,14 +66,3 @@ const test_exception = (function() { | 66 | 66 | 'Exception state did not remain clear as expected,' + | 67 | 67 | ` .wasPending() returned ${exception_pending}`); | 68 | 68 | } | 69 | | - | 70 | | -// Make sure that exceptions that occur during finalization are propagated. | 71 | | -function testFinalize(binding) { | 72 | | - let x = test_exception[binding](); | 73 | | - x = null; | 74 | | - assert.throws(() => { global.gc(); }, /Error during Finalize/); | 75 | | - | 76 | | - // To assuage the linter's concerns. | 77 | | - (function() {})(x); | 78 | | -} | 79 | | -testFinalize('createExternal'); |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +if (process.argv[2] === 'child') { | | 3 | + const common = require('../../common'); | | 4 | + // Trying, catching the exception, and finding the bindings at the `Error`'s | | 5 | + // `binding` property is done intentionally, because we're also testing what | | 6 | + // happens when the add-on entry point throws. See test.js. | | 7 | + try { | | 8 | + require(`./build/${common.buildType}/test_exception`); | | 9 | + } catch (anException) { | | 10 | + anException.binding.createExternal(); | | 11 | + } | | 12 | + | | 13 | + // Collect garbage 10 times. At least one of those should throw the exception | | 14 | + // and cause the whole process to bail with it, its text printed to stderr and | | 15 | + // asserted by the parent process to match expectations. | | 16 | + let gcCount = 10; | | 17 | + (function gcLoop() { | | 18 | + global.gc(); | | 19 | + if (--gcCount > 0) { | | 20 | + setImmediate(() => gcLoop()); | | 21 | + } | | 22 | + })(); | | 23 | + return; | | 24 | +} | | 25 | + | | 26 | +const assert = require('assert'); | | 27 | +const { spawnSync } = require('child_process'); | | 28 | +const child = spawnSync(process.execPath, [ | | 29 | + '--expose-gc', __filename, 'child' | | 30 | +]); | | 31 | +assert.strictEqual(child.signal, null); | | 32 | +assert.match(child.stderr.toString(), /Error during Finalize/m); |
Collapse file Copy file name to clipboardExpand all lines: test/js-native-api/test_general/test.js +26-26Lines changed: 26 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,46 +51,46 @@ assert.strictEqual(test_general.testGetVersion(), 6); | 51 | 51 | // for null | 52 | 52 | assert.strictEqual(test_general.testNapiTypeof(null), 'null'); | 53 | 53 |
| 54 | | -// Ensure that garbage collecting an object with a wrapped native item results | 55 | | -// in the finalize callback being called. | 56 | | -let w = {}; | 57 | | -test_general.wrap(w); | 58 | | -w = null; | 59 | | -global.gc(); | 60 | | -const derefItemWasCalled = test_general.derefItemWasCalled(); | 61 | | -assert.strictEqual(derefItemWasCalled, true, | 62 | | - 'deref_item() was called upon garbage collecting a ' + | 63 | | - 'wrapped object. test_general.derefItemWasCalled() ' + | 64 | | - `returned ${derefItemWasCalled}`); | 65 | | - | 66 | | - | 67 | 54 | // Assert that wrapping twice fails. | 68 | 55 | const x = {}; | 69 | 56 | test_general.wrap(x); | 70 | 57 | assert.throws(() => test_general.wrap(x), | 71 | 58 | { name: 'Error', message: 'Invalid argument' }); | | 59 | +// Clean up here, otherwise derefItemWasCalled() will be polluted. | | 60 | +test_general.removeWrap(x); | 72 | 61 |
| 73 | 62 | // Ensure that wrapping, removing the wrap, and then wrapping again works. | 74 | 63 | const y = {}; | 75 | 64 | test_general.wrap(y); | 76 | 65 | test_general.removeWrap(y); | 77 | 66 | // Wrapping twice succeeds if a remove_wrap() separates the instances | 78 | 67 | test_general.wrap(y); | 79 | | - | 80 | | -// Ensure that removing a wrap and garbage collecting does not fire the | 81 | | -// finalize callback. | 82 | | -let z = {}; | 83 | | -test_general.testFinalizeWrap(z); | 84 | | -test_general.removeWrap(z); | 85 | | -z = null; | 86 | | -global.gc(); | 87 | | -const finalizeWasCalled = test_general.finalizeWasCalled(); | 88 | | -assert.strictEqual(finalizeWasCalled, false, | 89 | | - 'finalize callback was not called upon garbage collection.' + | 90 | | - ' test_general.finalizeWasCalled() ' + | 91 | | - `returned ${finalizeWasCalled}`); | | 68 | +// Clean up here, otherwise derefItemWasCalled() will be polluted. | | 69 | +test_general.removeWrap(y); | 92 | 70 |
| 93 | 71 | // Test napi_adjust_external_memory | 94 | 72 | const adjustedValue = test_general.testAdjustExternalMemory(); | 95 | 73 | assert.strictEqual(typeof adjustedValue, 'number'); | 96 | 74 | assert(adjustedValue > 0); | | 75 | + | | 76 | +async function runGCTests() { | | 77 | + // Ensure that garbage collecting an object with a wrapped native item results | | 78 | + // in the finalize callback being called. | | 79 | + assert.strictEqual(test_general.derefItemWasCalled(), false); | | 80 | + | | 81 | + (() => test_general.wrap({}))(); | | 82 | + await common.gcUntil('deref_item() was called upon garbage collecting a ' + | | 83 | + 'wrapped object.', | | 84 | + () => test_general.derefItemWasCalled()); | | 85 | + | | 86 | + // Ensure that removing a wrap and garbage collecting does not fire the | | 87 | + // finalize callback. | | 88 | + let z = {}; | | 89 | + test_general.testFinalizeWrap(z); | | 90 | + test_general.removeWrap(z); | | 91 | + z = null; | | 92 | + await common.gcUntil( | | 93 | + 'finalize callback was not called upon garbage collection.', | | 94 | + () => (!test_general.finalizeWasCalled())); | | 95 | +} | | 96 | +runGCTests(); |
|