When I close a http server, why do I get a socket hang up?

I implemented a graceful stop to our node.js server. Basically something like this:

var shutDown = function () {
    server.on('close', function () {
        console.log('Server ' + process.pid + ' closed.');

        process.exit();
    });

    console.log('Shutting down ' + process.pid + '...');
    server.close();
}

However, when I close the server like this, I get a Error: socket hang up error in my continuous requests.

I thought that server.close() would make the server stop listening and accepting new requests, but keep processing all pending/open requests. However, that should result in an Error: connect ECONNREFUSED.

What am I doing wrong?

Additional info: The server consists of a master and three forked children/workers. However, the master is not listening or binding to a port, only the children are, and they are shut down as stated above.

Looking at the docs, it sounds like server.close() only stops new connections from coming in, so I'm pretty sure the error is with the already-open connections.

Maybe your shutDown() can check server.connections and wait until there are no more?

var shutDown = function(){
    if(server.connections) return setTimeout(shutDown, 1000);

    // Your shutDown code here
}

Slightly uglier (and much less scalable), but without the wait: you can keep track of connections as they occur and close them yourself

server.on('connection', function(e){
    // Keep track of e.connection in a list.
    //   You'll want to remove e.connection
    //   from the list if it closes on its own
}

var shutDown = function(){
    // Close all remaining connections in the list

    // Your shutDown code here
}