Skip to content

Instantly share code, notes, and snippets.

@bobrik
Created December 26, 2011 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobrik/1521447 to your computer and use it in GitHub Desktop.
Save bobrik/1521447 to your computer and use it in GitHub Desktop.
node.js incorrect net.Server.close behavior

This only happens on node.js 0.6.x, 0.4.x was fine. My node.js version is 0.6.6 (from homebrew, mac os x lion, but linux version is affected too). This may be not a but, maybe I just misread docs. Should it say that the server is closed when all connections destroyed?

What should you do to reproduce?

  1. In the first terminal tab:
node test-net.js
  1. In the second terminal tab:
telnet 127.0.0.1 1234
  1. In the first terminal tab ^C to call server.close (connection from the second tab is still alive)

  2. Server still not emit close event (this is weird, not?)

  3. Terminate telnet from the second terminal

  4. Server in the first tab emits close and successfully exit.

var net = require("net");
var server = net.createServer(function(socket) {
socket.on("data", function(data) {
socket.write(data);
});
});
server.listen(1234);
console.log("server started");
var firstSignal = true;
process.on("SIGINT", function() {
if (!firstSignal) {
return process.exit(0);
}
firstSignal = true;
server.on("close", function() {
console.log("server closed");
});
server.close();
console.log("server.close called");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment