From 5596f936d81b6309b9703d2d3fd4c129bb351672 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 28 Feb 2014 17:25:28 +0400 Subject: [PATCH] node: invoke `beforeExit` again if loop was active When `setImmediate(cb)` is called in `beforeExit` event handler the consequent `uv_run(..., UV_RUN_NOWAIT)` may return `0`, even if there was some active handles at start. Fixes simple/test-beforeexit-event.js. --- src/node.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 0a9a0cd81e..6accd4b5f2 100644 --- a/src/node.cc +++ b/src/node.cc @@ -3536,7 +3536,12 @@ int Start(int argc, char** argv) { more = uv_run(env->event_loop(), UV_RUN_ONCE); if (more == false) { EmitBeforeExit(env); - more = uv_run(env->event_loop(), UV_RUN_NOWAIT); + + // Emit `beforeExit` if the loop became alive either after emitting + // event, or after running some callbacks. + more = uv_loop_alive(env->event_loop()); + if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0) + more = true; } } while (more == true); code = EmitExit(env);