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

scope: disallow copying scopes to prevent double close of scopes #565

Closed
wants to merge 1 commit into from

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Oct 15, 2019

Early detect and prevent unexpected crashing on closing an already closed handle scope.

  • npm test passed

Copy link
Member

@NickNaso NickNaso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should prevent runtime error in case of double close. @legendecas Am I right?

@legendecas
Copy link
Member Author

legendecas commented Oct 15, 2019

I think that this should prevent runtime error in case of double close. @legendecas Am I right?

Yes, napi_handle_scope_mismatch would be returned if double closing a handle scope.

Though I've found that return statuses of napi_close_*_scope have been ignored 🤔 https://github.com/nodejs/node-addon-api/blob/master/napi-inl.h#L3475. Are these behavior expected?

@gabrielschulhof
Copy link
Contributor

@legendecas I believe the reason for that is that, if C++ exceptions are turned on, one would be throwing a C++ exception from a destructor which by my understanding is a big no-no.

@legendecas
Copy link
Member Author

Since no exceptions were expected to be thrown in the destructor, I'd think to disallow copying of scopes should be an intuitive approach. What do you think? @gabrielschulhof

@mhdawson
Copy link
Member

This would be a breaking change. While we've not said that will never happen for node-addon-api, we have chosen to avoid those in all cases before.

I think we'd want to have a really good reason to introduce a breaking change and I'm not sure it's going to be worth it in this case.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking as request changes until there is a discussion on whether making a breaking change is what we want to do.

@legendecas
Copy link
Member Author

@mhdawson: This would be a breaking change.

Technically it would depend on platforms and possibly crash on double closing a handle scope -- since in node core napi_handle_scope is just a pointer to be deleted on napi_close_handle_scope.

So I'd wonder there would hardly be possible any use case in the wild. But yes, it is a breaking change.

@legendecas
Copy link
Member Author

Append: node core has a pre-condition of deleting the HandleScopeWrapper*, so a napi_handle_scope_mismatch would be returned but not crashing immediately, which is what #566 was expected to address.

@legendecas
Copy link
Member Author

legendecas commented Oct 17, 2019

PPS: still, I could produce the crash with the following snippet:

Value doubleCloseScopeNapi(const CallbackInfo& info) {
  Env env = info.Env();
  HandleScope scope(env);
  HandleScope scope2(env);
  HandleScope scope3 = scope2;
  return env.Undefined();
}

Output:

node(30396,0x10cd83d40) malloc: *** error for object 0x104817990: pointer being freed was not allocated
node(30396,0x10cd83d40) malloc: *** set a breakpoint in malloc_error_break to debug

@mhdawson
Copy link
Member

Discussed in meeting today:

  1. team is comfortable that it is extremely unlikely this will break any existing code, as the code would already be crashing.
  2. V8 and NaN don't have the copy constructors as they don't make sense.

Consensus is we should we remove them.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@NickNaso NickNaso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mhdawson
Copy link
Member

We did discuss and this should land as a SemVer major even though it should not affect any existing code.

@NickNaso NickNaso mentioned this pull request Oct 22, 2019
15 tasks
@mhdawson
Copy link
Member

Landed as 34c11cf

@mhdawson mhdawson closed this Oct 23, 2019
@legendecas legendecas deleted the copy-scope branch October 26, 2019 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants