Skip to content

send on a closed WebSocket should not throw an exception #1515

Closed
@jfirebaugh

Description

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

Description

For compatibility with browser implementations, the HTML standard, and RFC 6455, calling send on a WebSocket whose readyState is CLOSING or CLOSED should discard the data and not throw an exception. (As a QOL issue, it may log an error to the console.)

Currently it throws an exception such as "WebSocket is not open: readyState 2 (CLOSING)".

Reproducible in:

  • version: 5.2.2
  • Node.js version(s): v10.14.1
  • OS version(s): macOS 10.14.3

Steps to reproduce:

  1. Start a WebSocket server on port 9000.

  2. Run the following in node:

const WebSocket = require("ws");
const ws = new WebSocket("ws://localhost:9000");
ws.onopen = () => {
  ws.close();
  ws.send("test");
  console.log("sent");
};
  1. Open the following in browsers:
<script>
const ws = new WebSocket("ws://localhost:9000");
ws.onopen = () => {
  ws.close();
  ws.send("test");
  console.log("sent");
};
</script>

Expected result:

All result in "sent" being printed to the console.

Actual result:

  • Node prints Error: WebSocket is not open: readyState 2 (CLOSING), with an exception stack trace pointing to websocket.js:320.
  • Chrome prints the error diagnostic "WebSocket is already in CLOSING or CLOSED state.", but does not throw an exception ("sent" is printed).
  • Firefox prints "sent" and no error.
  • Safari prints "sent" and no error.

References:

HTML Standard:

The send(data) method transmits data using the connection. If the readyState attribute is CONNECTING, it must throw an "InvalidStateError" DOMException. Otherwise, the user agent must run the appropriate set of steps from the following list:

Note that an exception must be thrown only in the CONNECTING state, and the "following list" does not include any steps that involve throwing an exception.

RFC 6455:

The endpoint MUST ensure the WebSocket connection is in the OPEN state (cf. Sections 4.1 and 4.2.2.) If at any point the state of the WebSocket connection changes, the endpoint MUST abort the following steps.

Note "abort the following steps"; i.e. stop processing, but do not raise an exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions