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

Firefox causes ws server to raise "TypeError: Cannot set property 'readyState' of undefined" (with MCVE) #1738

Closed
1 task done
canadaduane opened this issue Apr 15, 2020 · 8 comments

Comments

@canadaduane
Copy link

canadaduane commented Apr 15, 2020

  • I've searched for any related issues and avoided creating a duplicate issue.

Note: "Minimal, Complete, Reproducible Example" at http://github.com/canadaduane/ws-mcve-undefined-readystate

Description

When running express http and ws on the same port (and perhaps in a misconfigured way) Firefox will cause ws to fail with the following error:

Users/dujohnson/Projects/relm/server/mcve/node_modules/ws/lib/websocket.js:838
  websocket.readyState = WebSocket.CLOSING;
                       ^

TypeError: Cannot set property 'readyState' of undefined
    at Socket.socketOnClose (/Users/dujohnson/Projects/relm/server/mcve/node_modules/ws/lib/websocket.js:838:24)
    at Socket.emit (events.js:194:15)
    at TCP._handle.close (net.js:597:12)

Note that initiating a websocket connection from Chrome causes a different error:

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at writeAfterEnd (_stream_writable.js:243:12)
    at Receiver.Writable.write (_stream_writable.js:291:5)
    at Socket.socketOnData (/Users/dujohnson/Projects/relm/server/mcve/node_modules/ws/lib/websocket.js:876:35)
    at Socket.emit (events.js:194:15)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
Emitted 'error' event at:
    at Receiver.receiverOnError (/Users/dujohnson/Projects/relm/server/mcve/node_modules/ws/lib/websocket.js:781:13)
    at Receiver.emit (events.js:189:13)
    at writeAfterEnd (_stream_writable.js:245:10)
    at Receiver.Writable.write (_stream_writable.js:291:5)
    [... lines matching original stack trace ...]
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
error Command failed with exit code 1.

Reproducible in:

  • version: 7.2.3
  • Node.js version(s): 10.15.3
  • OS version(s): Mac OS 10.14.6
  • Firefox version: 74.0.1

Steps to reproduce:

  1. git clone git@github.com:canadaduane/ws-mcve-undefined-readystate.git

  2. yarn install

  3. yarn start and visit http://localhost:3000 in Firefox

Expected result:

Should raise a better error if I've misconfigured something, or no error at all if everything is OK.

Actual result:

After waiting up to 30 seconds, the above "TypeError: Cannot set property 'readyState' of undefined" crashes the server.

@lpinca
Copy link
Member

lpinca commented Apr 15, 2020

See #1704.

@lpinca
Copy link
Member

lpinca commented Apr 15, 2020

To fix the problem change this line https://github.com/canadaduane/ws-mcve-undefined-readystate/blob/master/minimal.js#L9

to

const wss = new WebSocket.Server({ noServer: true });

@lpinca lpinca closed this as completed Apr 15, 2020
@canadaduane
Copy link
Author

I put quite a bit of effort into showing how this error arises. I'd be gratified if future users of ws didn't accidentally run into the same (less than helpful) error message.

@lpinca
Copy link
Member

lpinca commented Apr 15, 2020

This commit 3641266 attempted to detect the invalid usage and provide a better message but your (and other) particular configurations are not covered.

I can think of way to catch all cases but the error message would be something like "The socket is already being used by another WebSocket" which is even more confusing for the end user, I guess.

@lpinca
Copy link
Member

lpinca commented Apr 15, 2020

By the way the bug report was top notch. Thanks.

@canadaduane
Copy link
Author

I think "The socket is already being used by another WebSocket" is actually a pretty good error in this case. I suspected my configuration was incorrect, but couldn't understand how the error message related to my config.

@canadaduane
Copy link
Author

Also, getting two different errors, dependent upon browser used, added to my confusion.

@lpinca
Copy link
Member

lpinca commented Apr 19, 2020

I've opened #1742. Suggestions and feedback are welcomed.

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

2 participants