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

src: remove redundant calls to HandleScope #20177

Closed
wants to merge 1 commit into from

Conversation

ryzokuken
Copy link
Contributor

Refs: #19972 (comment)
Refs: #20045

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

cc @addaleax @hashseed @nodejs/v8

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Apr 20, 2018
@ryzokuken ryzokuken self-assigned this Apr 20, 2018
@ryzokuken
Copy link
Contributor Author

@addaleax
Copy link
Member

I don’t think we can be sure that these calls are unnecessary. CallbackScope is part of the public API, and without these scopes, addons could theoretically run into problems with patterns like this:

HandleScope handle_scope(…);
for (int i = 0; i < n; i++) {
  CallbackScope callback_scope(…);
  HandleScope handle_scope(…);
  /* do something with Node.js objects here */
}

Omitting the callback scopes here means that all of the handles allocated by the constructor/destructor (I only see 2 that I’m sure of, but it’s hard to tell just by looking at the code) would fall into the outer scope now, and would accumulate and be kept alive for as long as that loop runs.

I don’t think the impact would be huge, but I would prefer to err on the side of caution and not remove these?

That being said: I am not sure whether there are handle allocations inside the InternalCallbackScope() constructor, and whether there are any before the env_->process_object() call in the close function. If there are none, then yes, we can remove these scopes (or, in the Close() case, move the scope to the part that deals with process.nextTick). A reliable way to check whether there are handle allocations or not would be to use a v8::SealHandleScope and see whether there’s a crash or not.

@ryzokuken
Copy link
Contributor Author

ryzokuken commented Apr 20, 2018

@addaleax I had been removing any calls to HandleScope preceding a call to Emit (and by extension, EmitBefore and EmitAfter) because we just started calling HandleScope within Emit in #20045.

That said, I'm really new to all this, and I might be very wrong. Let me know if I am, I would be more than happy to take my changes back.

@addaleax
Copy link
Member

@ryzokuken Yeah, and that totally makes sense for cases like #20045 where it’s obvious that the HandleScope we’d be removing does not capture any other handle allocations as well. But that’s not the case here, so things get more complicated…

@ryzokuken
Copy link
Contributor Author

@addaleax you're right. That said, if all the tests pass (benchmarks included), then perhaps it wouldn't be that bad of an idea anyway, right?

@addaleax
Copy link
Member

@ryzokuken Like I said, this may affect memory usage in some odd cases – The handle scopes aren’t quite as redundant as it was the case in #20045.

@ryzokuken
Copy link
Contributor Author

Hmm. Is there a way to find out? If they aren't redundant, we should just close this.

@addaleax
Copy link
Member

@ryzokuken Basically what I said above – replace them with SealHandleScopes and see what happens. In the Close() case there are definitely other handles being created, so you would have to add another handle scope later in the function to catch that.

@BridgeAR
Copy link
Member

Ping @ryzokuken

@ryzokuken
Copy link
Contributor Author

@BridgeAR @addaleax sorry I completely forgot about this. Please give me a few more days to take another stab at this.

@addaleax
Copy link
Member

@ryzokuken I think you can assume that this is not urgent in any way :)

@ryzokuken
Copy link
Contributor Author

@addaleax It definitely isn't. I'm using PRs like these to widen my understanding of the C++ part of the codebase, tbh. Need to know internals better.

@apapirovski
Copy link
Member

I'm going to close this out. Adding SealHandleScope results in crashes so clearly this isn't safe to do.

@apapirovski apapirovski closed this May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants