Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
src: don't check closing handles on beforeExit
uv_loop_alive() returns true if there are closing handles. This causes
an issue running the beforeExit event and a timer has been unref'd:

    process.on('beforeExit', function() {
      setTimeout(function() { }, 100).unref();
    });

The event loop also shouldn't run one additional time or else the timer
may execute an additional time.
  • Loading branch information
trevnorris committed Apr 1, 2015
commit 190af5eda3d6dac3b0b8c314968c5fe1f9a81137
12 changes: 6 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3824,21 +3824,21 @@ static void StartNodeInstance(void* arg) {

bool more;
do {
more = uv_run(env->event_loop(), UV_RUN_ONCE);
more = uv_run(env->event_loop(), UV_RUN_DEFAULT);
if (more == false) {
EmitBeforeExit(env);

// 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;
more = env->event_loop()->active_handles ||
(env->event_loop()->active_reqs[0] !=
env->event_loop()->active_reqs[1]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You're probably well aware of this but active_handles and active_reqs are implementation details.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah. That was done just to bring up the issue I'm working around, and the fact I can't figure out how to get around it.

}
} while (more == true);

int exit_code = EmitExit(env);
if (instance_data->is_main())
instance_data->set_exit_code(exit_code);
// TODO(trevnorris): This will fail for certain beforeExit cases.
//assert(!uv_loop_alive(env->event_loop()));
RunAtExit(env);

env->Dispose();
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-beforeexit-event-hang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require('assert');
var common = require('../common');
var i = 0;

process.on('beforeExit', function() {
assert.ok(++i <= 1);
setTimeout(function() { }, 100).unref();
});
2 changes: 1 addition & 1 deletion test/parallel/test-beforeexit-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ function tryListen() {
}

process.on('exit', function() {
assert.equal(4, deaths);
assert.equal(3, deaths);
assert.equal(3, revivals);
});