summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-11-26 17:09:27 +0100
committerAnna Henningsen <anna@addaleax.net>2019-11-28 17:49:18 +0100
commit4d73fd39489c6c2742cc16107f00dc81c7239706 (patch)
treeb53c6e4ded1019bdcc06687d961e8e3bfb7c0c7d /src
parent4c9db6c74150d3f5487798ca654496fdaf343422 (diff)
downloadandroid-node-v8-4d73fd39489c6c2742cc16107f00dc81c7239706.tar.gz
android-node-v8-4d73fd39489c6c2742cc16107f00dc81c7239706.tar.bz2
android-node-v8-4d73fd39489c6c2742cc16107f00dc81c7239706.zip
src: run native immediates during Environment cleanup
This can be necessary, because some parts of the Node.js code base perform cleanup operations in the Immediate callbacks, e.g. HTTP/2. This resolves flakiness in an HTTP/2 test that failed when a `SetImmediate()` callback was not run or destroyed before the `Environment` destructor started, because that callback held a strong reference to the `Http2Session` object and the expectation was that no such objects exist once the `Environment` constructor starts. Another, slightly more direct, alternative would have been to clear the immediate queue rather than to run it. However, this approach seems to make more sense as code generally assumes that the `SetImmediate()` callback will always run; For example, N-API uses an immediate callback to call buffer finalization callbacks. Unref’ed immediates are skipped, as the expectation is generally that they may not run anyway. Fixes: https://github.com/nodejs/node/issues/30643 PR-URL: https://github.com/nodejs/node/pull/30666 Refs: https://github.com/nodejs/node/pull/30374 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/env.cc8
-rw-r--r--src/env.h2
2 files changed, 7 insertions, 3 deletions
diff --git a/src/env.cc b/src/env.cc
index da253fac6d..9034c4e069 100644
--- a/src/env.cc
+++ b/src/env.cc
@@ -530,6 +530,8 @@ void Environment::CleanupHandles() {
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
+ RunAndClearNativeImmediates(true /* skip SetUnrefImmediate()s */);
+
for (ReqWrapBase* request : req_wrap_queue_)
request->Cancel();
@@ -645,7 +647,7 @@ void Environment::AtExit(void (*cb)(void* arg), void* arg) {
at_exit_functions_.push_front(ExitCallback{cb, arg});
}
-void Environment::RunAndClearNativeImmediates() {
+void Environment::RunAndClearNativeImmediates(bool only_refed) {
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
"RunAndClearNativeImmediates", this);
size_t ref_count = 0;
@@ -662,7 +664,9 @@ void Environment::RunAndClearNativeImmediates() {
if (head->is_refed())
ref_count++;
- head->Call(this);
+ if (head->is_refed() || !only_refed)
+ head->Call(this);
+
if (UNLIKELY(try_catch.HasCaught())) {
if (!try_catch.HasTerminated() && can_call_into_js())
errors::TriggerUncaughtException(isolate(), try_catch);
diff --git a/src/env.h b/src/env.h
index 495d92471a..78724cebc1 100644
--- a/src/env.h
+++ b/src/env.h
@@ -1415,7 +1415,7 @@ class Environment : public MemoryRetainer {
std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;
NativeImmediateCallback* native_immediate_callbacks_tail_ = nullptr;
- void RunAndClearNativeImmediates();
+ void RunAndClearNativeImmediates(bool only_refed = false);
static void CheckImmediate(uv_check_t* handle);
// Use an unordered_set, so that we have efficient insertion and removal.