joyent/node — pull request #6155 by bnoordhuis (2013-08-30 11:12:12+00:00)
Title: src: close libuv handles on exit

Commit 556b890 added a call to uv_loop_delete() with the intent of
catching handle lifecycle bugs. It worked because it exposed one:

process.on('exit', function() {
console.log('bye'); // Asserts.
});

When run, it asserts with the following message:

Assertion failed: (!uv__has_active_reqs(loop)), function
uv__loop_delete, file ../deps/uv/src/unix/loop.c, line 150.

That's because libuv as of joyent/libuv@3f2d4d5 checks that there are
no in-flight requests when the event loop is destroyed. In the test
case above, the write request for the string hasn't completed yet by
the time node.js exits: the string itself has most likely been written
but libuv hasn't had the opportunity to return the write request to
node.js.

That's why this commit adds a cleanup step right before exit where it
explicitly closes all open handles, then waits until the event loop
exits naturally (which on UNIX platforms is one or two ticks later.
On Windows, it's slightly more complicated.)

Fixes joyent/libuv#911.

Suggested reviewers: @isaacs @piscisaureus @tjfontaine