Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition prevents Node.js process from exiting #935

Closed
jamesshore opened this issue Jan 9, 2016 · 2 comments
Closed

Race condition prevents Node.js process from exiting #935

jamesshore opened this issue Jan 9, 2016 · 2 comments

Comments

@jamesshore
Copy link

When using socket.io-client in Node, if the server is closed too soon after the client connects, disconnect() doesn't work properly and the process never exits.

To reproduce, use this code with Node v5.4.0 and socket.io-client@1.4.3:

(function() {
  "use strict";

  var clientIo = require("socket.io-client");
  var http = require("http");
  var io = require('socket.io');

  var httpServer;
  var server;

  var PORT = 5020;

  startServer(function() {
    console.log("SERVER STARTED");

    var client = clientIo("http://localhost:" + PORT);

    client.on('connect', function() {
      client.disconnect();
      stopServer(function() {
        console.log("COMPLETE! NODE SHOULD EXIT NOW.");
      });
    });
  });

  function startServer(callback) {
    httpServer = http.createServer();
    server = io(httpServer);
    httpServer.listen(PORT, callback);
  };

  function stopServer(callback) {
    httpServer.on("close", function() {
      console.log("SERVER CLOSED");
      callback();
    });

    server.close();
  };

}());

Output:

$ node spikes/socket.io/test.js 
SERVER STARTED
SERVER CLOSED
COMPLETE! NODE SHOULD EXIT NOW.
^C

If you introduce a delay in the stopServer() function, everything works fine:

  function stopServer(callback) {
    httpServer.on("close", function() {
      console.log("SERVER CLOSED");
      callback();
    });

    setTimeout(function() {
      server.close();
    }, 500);
  };

Output:

$ node spikes/socket.io/test.js 
SERVER STARTED
SERVER CLOSED
COMPLETE! NODE SHOULD EXIT NOW.
$
@mspoulsen
Copy link

Hi James,

I added two lines and now it should exit. I have no clue why though :)

(function() {
    "use strict";

    var clientIo = require("socket.io-client");
    var http = require("http");
    var io = require('socket.io');

    var httpServer;
    var server;

    var PORT = 5020;

    startServer(function() {
        console.log("SERVER STARTED");

        // added options object
        var client = clientIo("http://localhost:" + PORT, { transports: ['websocket'] });

        client.on('connect', function() {
            client.disconnect();

            stopServer(function() {
                console.log("COMPLETE! NODE SHOULD EXIT NOW.");
            });
        });
    });

    function startServer(callback) {
        httpServer = http.createServer();
        server = io(httpServer);
        httpServer.listen(PORT, callback);
    }

    function stopServer(callback) {
        // added server close
        server.close();         
        httpServer.on("close", function() {
            console.log("SERVER CLOSED");
            callback();
        });

    }

}());

@nkzawa
Copy link
Contributor

nkzawa commented Jan 29, 2016

Should be fixed on v1.4.5. Related socketio/socket.io#2069

@nkzawa nkzawa closed this as completed Jan 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants