socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

JS exceptions thrown while just idling (WebSocket is already in CLOSING or CLOSED state.)

Open getify opened this issue 8 years ago • 60 comments

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

While leaving a socket connected, but doing no other activity in my program, I see intermittently (but fairly consistently) an exception thrown in browser console, from inside of the socket.io machinery (specifically in backo2/index.js line 83. That error is:

WebSocket is already in CLOSING or CLOSED state.

socket-io-errors

Steps to reproduce (if the current behaviour is a bug)

I have two tabs open with client sockets connected to the same server (localhost) via https. Both clients are sitting idle with nothing else happening in browser or server (except for whatever keep-alive pings socket.io is doing). Both of them are joined to a single channel (via join(..) on the server). Otherwise, nothing else special.

Here's how I create the server socket instance:

var httpsServer = https.createServer(..);
var io = require("socket.io")(httpsServer);
io.on("connection",onSocketConnection);

And in the client:

socket = io();
socket.on("connect",function(){
   console.log("socket connected");
});
socket.on("disconnect",function(){
   console.log("socket disconnected");
});

Expected behaviour

I expect disconnections and reconnections from time to time, but I don't expect spurious JS exceptions thrown by the library when I'm not doing anything else over the connections.

Setup

  • OS: Mac OSX
  • browser: Chrome 66, node 10.2.1
  • socket.io version: 2.1.1

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Expanded stack trace:

index.js:83 WebSocket is already in CLOSING or CLOSED state.
(anonymous) @ index.js:83
e.encodePacket @ index.js:83
(anonymous) @ index.js:83
r.write @ index.js:83
r.send @ index.js:83
r.flush @ index.js:83
r.sendPacket @ index.js:83
r.ping @ index.js:83
(anonymous) @ index.js:83
setTimeout (async)
r.setPing @ index.js:83
r.onPacket @ index.js:83
(anonymous) @ index.js:83
r.emit @ index.js:83
r.onPacket @ index.js:83
r.onData @ index.js:83
ws.onmessage @ index.js:83

getify avatar May 26 '18 16:05 getify

Experiencing absolutely the same issue and the code is correct. This only happens in Chrome for me. Mozilla is clean. In chrome, this err is raining over and over and duplicating all the chats I have. I tried using this method socket.on('disconnect', () =>{ socket.disconnect(); }); It doesn't disconnect the client from the server. Repo in case needed https://github.com/antoniab123456/Chat_app

antoniab123456 avatar Jun 08 '18 09:06 antoniab123456

the same problem here image

browser is just idling, then the errors come up.

I used Chrome and mac OS

danghung-dev avatar Jun 13 '18 16:06 danghung-dev

Yes guys, can someone address that? Perhaps we need another line of code to fix that?

antoniab123456 avatar Jun 16 '18 15:06 antoniab123456

I have the same problem with socket.io and Chrome Mac OS 10.13.5 Chrome Version 67.0.3396.87 (Official Build) (64-bit) Node: 10.3.0 Express: 4.16.3 socket.io: 2.1.1

In Firefox everything is fine.

Error details below:

index.js:83 WebSocket is already in CLOSING or CLOSED state.
(anonymous) | @ | index.js:83
  | e.encodePacket | @ | index.js:83
  | (anonymous) | @ | index.js:83
  | r.write | @ | index.js:83
  | r.send | @ | index.js:83
  | r.flush | @ | index.js:83
  | r.sendPacket | @ | index.js:83
  | r.ping | @ | index.js:83
  | (anonymous) | @ | index.js:83
  | setTimeout (async) |   |  
  | r.setPing | @ | index.js:83
  | r.onPacket | @ | index.js:83
  | (anonymous) | @ | index.js:83
  | r.emit | @ | index.js:83
  | r.onPacket | @ | index.js:83
  | r.onData | @ | index.js:83
  | ws.onmessage | @ | index.js:83

When I click on the index.js:83 it takes me to this module:

/**
 * Expose `Backoff`.
 */

module.exports = Backoff;

/**
 * Initialize backoff timer with `opts`.
 *
 * - `min` initial timeout in milliseconds [100]
 * - `max` max timeout [10000]
 * - `jitter` [0]
 * - `factor` [2]
 *
 * @param {Object} opts
 * @api public
 */

function Backoff(opts) {
  opts = opts || {};
  this.ms = opts.min || 100;
  this.max = opts.max || 10000;
  this.factor = opts.factor || 2;
  this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0;
  this.attempts = 0;
}

/**
 * Return the backoff duration.
 *
 * @return {Number}
 * @api public
 */

Backoff.prototype.duration = function(){
  var ms = this.ms * Math.pow(this.factor, this.attempts++);
  if (this.jitter) {
    var rand =  Math.random();
    var deviation = Math.floor(rand * this.jitter * ms);
    ms = (Math.floor(rand * 10) & 1) == 0  ? ms - deviation : ms + deviation;
  }
  return Math.min(ms, this.max) | 0;
};

/**
 * Reset the number of attempts.
 *
 * @api public
 */

Backoff.prototype.reset = function(){
  this.attempts = 0;
};

/**
 * Set the minimum duration
 *
 * @api public
 */

Backoff.prototype.setMin = function(min){
  this.ms = min;
};

/**
 * Set the maximum duration
 *
 * @api public
 */

Backoff.prototype.setMax = function(max){
  this.max = max;
};

/**
 * Set the jitter
 *
 * @api public
 */

Backoff.prototype.setJitter = function(jitter){
  this.jitter = jitter;
};




//////////////////
// WEBPACK FOOTER
// ./~/backo2/index.js
// module id = 41
// module chunks = 0

Line 83 is:

this.jitter = jitter;

dtroydev avatar Jun 19 '18 03:06 dtroydev

I sort of have the same problem, with backo2.js and in line 83. here is my error: index.js:83 Uncaught TypeError: Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'. at n (index.js:83) at n (index.js:83) at n (index.js:83) at n (index.js:83) at n (index.js:83) at n (index.js:83) at Object.e.removeBlobs (index.js:83) at s (index.js:83) at r.encode (index.js:83) at r.packet (index.js:83) and dont worry about the other stuff, they all are in line 83, which is this.jitter = jitter; It is annoying me so much as there isnt really any solution in google.

ghost avatar Jul 02 '18 10:07 ghost

Has anyone found a temporary solution? @antoniab123456 were you able to fix your chat app so that it doesn't duplicate your chats? It's doing the same thing to me.

Setup OS: Mac OSX Browser: Chrome 67.0.3396.99 (Official Build) (64-bit) Node v10.5.0 Socket.io v2.1.1

garrrettt avatar Jul 02 '18 21:07 garrrettt

I am facing the exact problem @getify mentioned in chrome. Has any one found any work around for this?

vkotu avatar Jul 15 '18 01:07 vkotu

Same problem. Can someone please respond to this?

gavin310 avatar Jul 17 '18 16:07 gavin310

Same problem.

Setup OS: Mac OSX browser: Chrome 67.0.3396.99 (Official Build) (64-bit), Node 9.6.1 socket.io version: 2.1.1

amerej avatar Jul 18 '18 08:07 amerej

@19smitgr idk what you mean by 'dulplicate chats' which sounds off topic which could be solved by broadcasting instead if you mean 'duplicate message in same screen'.

ghost avatar Jul 20 '18 04:07 ghost

@kino2007 I'm not making a chat application. I'm actually using websockets with a multiplayer game, and the websocket wasn't getting a disconnect message, so when the user disconnected and reconnected due to the random error that everyone is talking about, it would give the character a new socket ID, and the old sprite with the old socket ID wasn't deleted because it was just getting a "transport close" message instead of a "disconnect" message.

At this point, even if I get a "transport close" message, I just delete the user from my list of current users the same way as if I got a "disconnect" message.

garrrettt avatar Jul 20 '18 17:07 garrrettt

Same error here

After a quick search I found out that backo2 causes the error.

Then I used the dev version of socket.io and found out that the Socket.prototype.onevent throws the error.

provokateurin avatar Jul 29 '18 18:07 provokateurin

I fixed it for me:

Previously I used this:

socket.on('ping', alert);

But then I changed it to this:

socket.on('ping', msg => {
    alert(msg);
});

And it worked!

provokateurin avatar Jul 29 '18 18:07 provokateurin

I'm having the same problem. Any updates on this?

IainM22 avatar Aug 22 '18 20:08 IainM22

Same here.

socket.io & socket.io-client: "^2.1.1" MacOS Google Chrome is up to date Version 68.0.3440.106 (Official Build) (64-bit)

Evgenyx82 avatar Aug 24 '18 10:08 Evgenyx82

I am having the same issue. Also, it's duplicating messages I send from server to client.

screen shot 2018-08-30 at 4 51 49 pm

MacOS: 10.13.6 Socket.io: "^2.1.1" Chrome: 68.0.3440.106

abhyuditjain avatar Aug 30 '18 11:08 abhyuditjain

@abhyuditjain You can try reset all the listener's using socket.removeAllListeners(); to avoid multiple listener registrations which might cause the duplicating messages.

vkotu avatar Aug 30 '18 18:08 vkotu

This seems to be an ongoing issue for several people and there has not been any solution to this. Any updates from anyone?

Asher978 avatar Sep 04 '18 00:09 Asher978

@vkotu If I remove all the listeners, then the socket doesn't reconnect to server on disconnect. Any way to fix this?

abhyuditjain avatar Sep 04 '18 06:09 abhyuditjain

just use websocket- node, it is easy to use and works without any errors and plus you don't need an external library for that: https://codeburst.io/why-you-don-t-need-socket-io-6848f1c871cd https://medium.com/@martin.sikora/node-js-websocket-simple-chat-tutorial-2def3a841b61 https://www.npmjs.com/package/websocket

antoniab123456 avatar Sep 05 '18 09:09 antoniab123456