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

async_wrap: close the destroy_ids_idle_handle_ on environment destruction #10385

Closed
wants to merge 3 commits into from
Closed

Conversation

reshnm
Copy link
Contributor

@reshnm reshnm commented Dec 21, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

async_wrap

Description of change

The destroy_ids_idle_handle_ is currently not getting closed in the environment destructor. After the environment has been destroyed, the uv loop used by the environment contains a dangling pointer in the handle list which leads to undefined behavior when the loop is still being used.

This is currently no issue for the "main" uv loop used by node. However if an embedder creates his own environment this will most likely lead to crashes. By looking at the code in Environment::Start it was also not entirely clear for me that destroy_ids_idle_handle_ is not added to the handle cleanup queue by intention.

The additional delayed cleanup queue may be an overkill since it only contains one handle right now, but it seemed to me that it would be the most generic and verbose approach.

The destroy_ids_idle_handle_ needs to be closed on environment destruction.
Not closing the handle leaves a dangling pointer in the used uv loop.
This leads to undefined behavior when the uv loop is used after
the environment has been destroyed.
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. dont-land-on-v7.x labels Dec 21, 2016
@reshnm reshnm changed the title [async_wrap] close the destroy_ids_idle_handle_ on environment destruction async_wrap: close the destroy_ids_idle_handle_ on environment destruction Dec 21, 2016
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Seems okay but I agree that this approach looks a bit like overkill, let’s hear what others think.

@bnoordhuis
Copy link
Member

A rule of thumb I use is "don't generalize until there are at least two or three instances." In this particular case, I'd just close the idle handle last in the destructor.

Aside: am I to infer SAP uses node in their products? It sure is a step up from ABAP. :-)

(I spent most of 2004 writing ABAP. I don't hate it, actually.)

Do not add the destroy_ids_idle_handle_ to a separate handle clenup queue.
Call uv_close on the handle directly in the environment destructor.
@reshnm
Copy link
Contributor Author

reshnm commented Dec 22, 2016

Good point. I moved the cleanup code completely to the environment destructor.

@bnoordhuis You are right, we use node in our products. Your ABAP programming skills are probably better than mine. I have never written a single line of code in ABAP :-)

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

Member handle_cleanup_waiting_ must be set to one before
the uv_close on destroy_ids_idle_handle_ is called.
jasnell pushed a commit that referenced this pull request Dec 24, 2016
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: #10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@jasnell
Copy link
Member

jasnell commented Dec 24, 2016

Landed in c7ff96b

@jasnell jasnell closed this Dec 24, 2016
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 18, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 19, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 24, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this pull request Jan 27, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@italoacasas italoacasas mentioned this pull request Jan 29, 2017
@MylesBorins
Copy link
Contributor

@trevnorris should this be backported? Do you want to do a batch update to async_wrap across the LTS lines?

@MylesBorins
Copy link
Contributor

ping @trevnorris

@MylesBorins
Copy link
Contributor

ping

addaleax pushed a commit to addaleax/node that referenced this pull request May 9, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@addaleax
Copy link
Member

addaleax commented May 9, 2017

backport in #12922

MylesBorins pushed a commit that referenced this pull request May 16, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: #10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request May 18, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: #10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorins MylesBorins mentioned this pull request May 23, 2017
andrew749 pushed a commit to michielbaird/node that referenced this pull request Jul 19, 2017
The destroy_ids_idle_handle_ needs to be closed on
environment destruction. Not closing the handle leaves
a dangling pointer in the used uv loop. This leads to
undefined behavior when the uv loop is used after the
environment has been destroyed.

PR-URL: nodejs/node#10385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
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.

8 participants