Skip to content

Closing a server does not disconnect clients #495

Closed
@josephg

Description

@josephg

This server calls s.server.close() 1 second after a client connects.

io = require 'socket.io'

s = io.listen 4321

s.sockets.on 'connection', (socket) ->
    setTimeout (-> s.server.close()), 1000

... however, the client is never disconnected:

$ coffee server.coffee 
   info  - socket.io started
   debug - client authorized
   info  - handshake authorized 4096240811453695407
   debug - setting request GET /socket.io/1/websocket/4096240811453695407
   debug - set heartbeat interval for client 4096240811453695407
   debug - client authorized for 
   debug - websocket writing 1::
new connection
client connected. Server will close in 1 second...
   debug - emitting heartbeat for client 4096240811453695407
   debug - websocket writing 2::
   debug - set heartbeat timeout for client 4096240811453695407
   debug - websocket received data packet 2::
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client 4096240811453695407
   debug - set heartbeat interval for client 4096240811453695407
   debug - emitting heartbeat for client 4096240811453695407
   debug - websocket writing 2::
   debug - set heartbeat timeout for client 4096240811453695407
   debug - websocket received data packet 2::
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client 4096240811453695407
   debug - set heartbeat interval for client 4096240811453695407
   debug - emitting heartbeat for client 4096240811453695407
   debug - websocket writing 2::

....etc

Activity

3rd-Eden

3rd-Eden commented on Sep 7, 2011

@3rd-Eden
Contributor

Isn't that Node.js core issue instead of a Socket.IO issue? I would say that Node should clean up all fd's when you want to close the server.

josephg

josephg commented on Sep 7, 2011

@josephg
Author

Quite possibly, especially considering the server -still doesn't quit even after the client has disconnected- (Actually, it does quit, exactly 15 seconds later). If the listening socket were closed but the client socket wasn't closed, you'd expect closing the client socket to terminate the process at last.

josephg

josephg commented on Sep 7, 2011

@josephg
Author

I just played with it -

Given this server:

server = require('http').createServer (req, res) ->
    res.writeHead 200, 'Content-Type': 'text/plain'
    res.write 'Hello world\n'

    setTimeout (-> console.log 'closing'; server.close()), 1000

server.listen 4321

lazy client:

% curl localhost:4321

Just like with the socket.io code:

  • server.close() causes the server's listening socket to be closed immediately
  • The app doesn't close at this point (closing the server socket does not close the client socket).

With the native node.js app, the server terminates as soon as the last client connection is closed. With socket.io, the server still closes on its own, but it waits a full 15 seconds after the last client manually disconnects and the process terminates.

I think node.js is probably acting correctly according to its spec. It sort of makes sense to deal with the last http requests properly while a server is shutting down. .. But I don't think the behaviour is the right one in socket.io. (Terminating all the open connections is certainly what I expected to happen when I called server.close()).

3rd-Eden

3rd-Eden commented on Sep 19, 2011

@3rd-Eden
Contributor

Yes and no, we can just loop over all clients and send a disconnect() packet. So I'm marking this as bug.

setthase

setthase commented on Jun 26, 2013

@setthase

Does somebody look at this after so long time? Is this on roadmap for one of next release?

jtmalinowski

jtmalinowski commented on Jan 10, 2014

@jtmalinowski

dang, I just hit this issue when writing tests

samsonradu

samsonradu commented on Jan 10, 2014

@samsonradu

@JakubMal I'm interested in seeing the test. Could you please post it?

jtmalinowski

jtmalinowski commented on Jan 10, 2014

@jtmalinowski

@samsonradu sorry man, my bad! Should have checked twice...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @3rd-Eden@josephg@jtmalinowski@setthase@samsonradu

        Issue actions

          Closing a server does not disconnect clients · Issue #495 · socketio/socket.io