src: close libuv handles on exit
authorBen Noordhuis <info@bnoordhuis.nl>
Thu, 29 Aug 2013 09:39:37 +0000 (11:39 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Fri, 30 Aug 2013 16:39:37 +0000 (18:39 +0200)
commit4915884da69814bd4daab22393919a628c9ecf23
treec697154546337cfbd5108e40d98a4a0df9c873c8
parente83a0cd016ef03d3ec3b491325b347be886c1476
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.

Named pipes (UNIX domain sockets) are shut down first in order to flush
pending write requests.  Should go some way towards fixing the Windows
issue where output on stdout/stderr sometimes gets truncated.

Fixes joyent/libuv#911.
src/node.cc
test/simple/test-process-exit-print.js [new file with mode: 0644]