socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.09k stars 10.11k forks source link

Socket.io server crash on client connect @1.7.3 due to bufferutil and utf-8-validate #2891

Closed sergei1152 closed 7 years ago

sergei1152 commented 7 years ago

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

Current behaviour

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

1) Start headless socket.io server with:

require('dotenv').config();
var Server = require('socket.io');

var io = new Server();

io.on('connection', function(client){

  console.log("a new connection was received!");

client.on('request_payload', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  client.on('disconnect', function(){
    console.error("Client disconnected!");
  })

});

console.log(`Started socket server on port ${process.env.SOCKET_SERVER_PORT}`);

io.listen(process.env.SOCKET_SERVER_PORT);

2) Attempt client side connection with socket.io-client@1.7.3

         var socket = io(`ws://127.0.0.1:9090/`);
         socket.on('connect', function(){
             console.log('connected!');
         });
  socket.on('event', function(data){});
  socket.on('disconnect', function(){
      console.log('disconnected');
  });

Note that I'm using webpack as my build system, and taking in socket.io-client through an import instead of it being served down in the /socket.io route like it usually should, though I doubt this should matter.

The server runs fine if there are no incoming connections. As soon as a client tries to connect I get the issue seen below.

Expected behaviour

Not crash

Setup

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

Node version: 7.5.0

> node socketapp/server.js
Started socket server on port 9090
a new connection was received!
C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\Receiver.js:306
  if (mask != null && buf != null) bufferUtil.unmask(buf, mask);
                                             ^

TypeError: Cannot read property 'unmask' of undefined
    at Receiver.unmask (C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\Receiver.js:306:46)
    at Receiver.finish (C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\Receiver.js:505:25)
    at Receiver.expectHandler (C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\Receiver.js:493:33)
    at Receiver.add (C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\Receiver.js:103:24)
    at Socket.realHandler (C:\Users\Coop\Documents\Github\lora_demo_server\node_modules\ws\lib\WebSocket.js:825:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)
sergei1152 commented 7 years ago

It looks like this is an issue with the ws module. Installing the bufferutil and utf-8-validate modules, as per reccomendation on their npm page breaks the module. The issue seems to be that the api that they use for the mocks doesnt match the api for the most updated versions of these modules. Weird because they list the most updated version of these in their package.json.

Performing an npm remove bufferutil and npm remove utf-8-validate after installation of socket.io seemed to have resolved the issue.

sergei1152 commented 7 years ago

There is a PR for this in engine.io as it seems: https://github.com/socketio/engine.io/pull/487

brenc commented 7 years ago

Installing the versions ws@1.1.2 requires worked for me:

bufferutil@1.2.x
utf-8-validate@1.2.x
lpinca commented 7 years ago

You can use bufferutil@<2 and utf-8-validate@<3 with ws@1.

sergei1152 commented 7 years ago

Removing them completely is something that also worked, though this should probably be documented in the README, as it was an annoying issue to resolve.

sergei1152 commented 7 years ago

At least until ws is upgraded in engine.io

darrachequesne commented 7 years ago

@sergei1152 is there anything else we can do here?

lpinca commented 7 years ago

@darrachequesne we released ws@1.1.4 with support for all bufferutil and utf-8-validate versions.

darrachequesne commented 7 years ago

@lpinca awesome, as usual. I guess we can close the issue then, thanks.