WebSocket-Node icon indicating copy to clipboard operation
WebSocket-Node copied to clipboard

Connection not closed (as stated in version 1.0.9 - 2014/10/20)

Open quanguyen opened this issue 8 years ago • 5 comments

Hi,

Thanks for the great work on this websocket library. I am using version 1.0.22 (I know this thank to the first lines in CHANGELOG.md and README.md). After a longtime running, I see that the server still keeps a connection that is supposed to have been dead (because I explicitly powered off the client). Reading my logs, I see that connection never sends close event to my code although the client was turned off.

Reviewing in CHANGELOG.md, I learnt that this problem has been fixed since version 1.0.9, released on 2014/10/20. Somehow this zombie connection issue is not yet completely fixed?

Please let me know:

  • Am I right to determine my current websocket version by looking at the first lines of CHANGELOG.md and README.md? I must clarify this important point as I am afraid I am using a version lower than 1.0.9.
  • Has anyone reported this zombie connection since version 1.0.9?
  • How do I fix this problem?

Thanks,

quanguyen avatar Apr 19 '16 07:04 quanguyen

Am I right to determine my current websocket version by looking at the first lines of CHANGELOG.md and README.md? I must clarify this important point as I am afraid I am using a version lower than 1.0.9.

No, you should query the version property:

console.log(require('websocket').version)

Alternately, look at the version specified in package.json.

Has anyone reported this zombie connection since version 1.0.9?

Not that I'm aware of.

How do I fix this problem?

I don't know nearly enough about your problem, your code, or your environment to have any hope of answering this question. :-(

If possible please provide a specific test case that can reliably reproduce the behavior that you're seeing and then we can investigating. However, I suspect you are using the library incorrectly.

theturtle32 avatar Apr 20 '16 00:04 theturtle32

Hi Brian,

The version is 1.0.22. I verified by printing out:

console.log(require('websocket').version)

Below is my code outline:

"use strict"

const PORT = SOME_PORT

var http = require("http")
var server = http.createServer(function(req, resp) {
    // Do nothing
}).listen(PORT, function() {
    console.log((new Date()) + " Server is listening on port " + PORT)
})

var connections = []

var webSocketServer = require("websocket").server
var wsServer = new webSocketServer( {
    httpServer: server
})
wsServer.on("request", function(request) {
    var connection = request.accept(null, request.origin)

    connection.on("message", function(message) {
       // Do some logic...
// If "connection" is not found in "connections", then connections.push(connection) // A
// else send some message to the client on connection; connection.close() // B

    })

    connection.on("close", function(code, description) {
        // find "connection" index in "connections", then connections.splice(index, 1)
    })
})

After a longtime running in about a week, one of my client connections keeps disconnecting due to B when it tries to reconnect after disconnection. I know for sure the code falls into B because the client does receive the message before the connection is dropped. Obviously, the code falls into B since connection still exists in connections (i.e. close event has not been emitted).

Note that, close event works quite well in very first times, but things will happen when it runs in a long time (or maybe memory gets bigger?).

Regards,

quanguyen avatar Apr 20 '16 03:04 quanguyen

Try pushing your connection in the request handler, not the message handler.

theturtle32 avatar Jun 16 '16 20:06 theturtle32

No, I cannot push my connection in the request handler, which is too early. Before pushing to connections, I need to verify it with // Do some logic.... If I fail to verify, then I will not push it into the connections.

quanguyen avatar Jun 17 '16 03:06 quanguyen