summaryrefslogtreecommitdiff
path: root/src/env.cc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-09-04 22:02:55 +0200
committerAnna Henningsen <anna@addaleax.net>2018-05-10 14:15:16 +0200
commit17e289eca8f8398243df5c4006d80f7381fd08bc (patch)
tree781f1398f78aa4b602ae3fca8654c28576e70b52 /src/env.cc
parent5c6cf30143f3191b043ba0b4e814768efa1069f7 (diff)
downloadandroid-node-v8-17e289eca8f8398243df5c4006d80f7381fd08bc.tar.gz
android-node-v8-17e289eca8f8398243df5c4006d80f7381fd08bc.tar.bz2
android-node-v8-17e289eca8f8398243df5c4006d80f7381fd08bc.zip
src: make CleanupHandles() tear down handles/reqs
Previously, handles would not be closed when the current `Environment` stopped, which is acceptable in a single-`Environment`-per-process situation, but would otherwise create memory and file descriptor leaks. Also, introduce a generic way to close handles via the `Environment::CloseHandle()` function, which automatically keeps track of whether a close callback has been called yet or not. Many thanks for Stephen Belanger for reviewing the original version of this commit in the Ayo.js project. Refs: https://github.com/ayojs/ayo/pull/85 PR-URL: https://github.com/nodejs/node/pull/19377 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/env.cc')
-rw-r--r--src/env.cc20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/env.cc b/src/env.cc
index aadb81092e..6526c680ac 100644
--- a/src/env.cc
+++ b/src/env.cc
@@ -209,9 +209,7 @@ void Environment::RegisterHandleCleanups() {
void* arg) {
handle->data = env;
- uv_close(handle, [](uv_handle_t* handle) {
- static_cast<Environment*>(handle->data)->FinishHandleCleanup(handle);
- });
+ env->CloseHandle(handle, [](uv_handle_t* handle) {});
};
RegisterHandleCleanup(
@@ -233,13 +231,17 @@ void Environment::RegisterHandleCleanups() {
}
void Environment::CleanupHandles() {
- for (HandleCleanup& hc : handle_cleanup_queue_) {
- handle_cleanup_waiting_++;
+ for (ReqWrap<uv_req_t>* request : req_wrap_queue_)
+ request->Cancel();
+
+ for (HandleWrap* handle : handle_wrap_queue_)
+ handle->Close();
+
+ for (HandleCleanup& hc : handle_cleanup_queue_)
hc.cb_(this, hc.handle_, hc.arg_);
- }
handle_cleanup_queue_.clear();
- while (handle_cleanup_waiting_ != 0)
+ while (handle_cleanup_waiting_ != 0 || !handle_wrap_queue_.IsEmpty())
uv_run(event_loop(), UV_RUN_ONCE);
}
@@ -306,6 +308,8 @@ void Environment::PrintSyncTrace() const {
}
void Environment::RunCleanup() {
+ CleanupHandles();
+
while (!cleanup_hooks_.empty()) {
// Copy into a vector, since we can't sort an unordered_set in-place.
std::vector<CleanupHookCallback> callbacks(
@@ -329,8 +333,8 @@ void Environment::RunCleanup() {
cb.fn_(cb.arg_);
cleanup_hooks_.erase(cb);
- CleanupHandles();
}
+ CleanupHandles();
}
}