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: napi_wrap-ped finalize_cb not invoked #22396

Closed
legendecas opened this issue Aug 19, 2018 · 8 comments
Closed

n-api: napi_wrap-ped finalize_cb not invoked #22396

legendecas opened this issue Aug 19, 2018 · 8 comments
Labels
node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests.

Comments

@legendecas
Copy link
Member

  • Version: 10.9.0
  • Platform: macOS
  • Subsystem: n-api

It seems napi_wraped object's finalize_cb is not called on test case: https://github.com/nodejs/node/blob/v10.9.0/test/addons-napi/7_factory_wrap/myobject.cc#L8

Yet if we wrap the JS code https://github.com/nodejs/node/blob/v10.9.0/test/addons-napi/7_factory_wrap/test.js#L6-L14 with a function context and force gc like following:

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

function context() {
  const obj = createObject(10);
  assert.strictEqual(obj.plusOne(), 11);
  assert.strictEqual(obj.plusOne(), 12);
  assert.strictEqual(obj.plusOne(), 13);

  const obj2 = createObject(20);
  assert.strictEqual(obj2.plusOne(), 21);
  assert.strictEqual(obj2.plusOne(), 22);
  assert.strictEqual(obj2.plusOne(), 23);
}
context();
global.gc();

the finalize_cb does be called.

@mhdawson
Copy link
Member

I think you are suggesting we should update the test, I agree? Do you want to open a PR to do that?

@legendecas
Copy link
Member Author

Yes, if possible. But I didn't figure out why in bare context objects' finalize callback was not invoked, since objects should be gc-ed on exit right?

@addaleax addaleax added test Issues and PRs related to the tests. node-api Issues and PRs related to the Node-API. labels Aug 24, 2018
@mhdawson
Copy link
Member

I don't think there is any requirement for a gc on exit. Unless there is somewhere in the JS spec that says finalizers will be invoked before exit (my guess is that there is not) then the process can simply exist and all memory is freed so no reason to run a gc first.

@legendecas
Copy link
Member Author

Sounds reasonable to me that there is no reason to run gc first before exit.

More question: whether finalize callback shall be ensured to be invoked in this test case? IMO there are reasons to test it to cover napi wraps.

@mhdawson
Copy link
Member

It would be would to have the test case cover the callback being invoked if possible. Do you want to update PR to update the test to validate that?

@legendecas
Copy link
Member Author

@mhdawson PR is submitted :p

@gabrielschulhof
Copy link
Contributor

@legendecas the finalizer will be called if you do

let obj1 = createObject(10);
obj1 = null;
global.gc();

so this is not actually a bug.

@gabrielschulhof
Copy link
Contributor

@legendecas we actually check exactly that the finalizer gets called in https://github.com/nodejs/node/blob/master/test/addons-napi/8_passing_wrapped/test.js.

legendecas added a commit to legendecas/node that referenced this issue Sep 11, 2018
gabrielschulhof pushed a commit that referenced this issue Sep 13, 2018
Fixes: #22396
PR-URL: #22612
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
targos pushed a commit that referenced this issue Sep 14, 2018
Fixes: #22396
PR-URL: #22612
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
targos pushed a commit that referenced this issue Sep 19, 2018
Fixes: #22396
PR-URL: #22612
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
targos pushed a commit that referenced this issue Sep 20, 2018
Fixes: #22396
PR-URL: #22612
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests.
Projects
None yet
Development

No branches or pull requests

4 participants