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

Clear the send queue when the other peer closes the connection #1226

Closed
lpinca opened this issue Oct 31, 2017 · 0 comments
Closed

Clear the send queue when the other peer closes the connection #1226

lpinca opened this issue Oct 31, 2017 · 0 comments

Comments

@lpinca
Copy link
Member

lpinca commented Oct 31, 2017

Description

permessage-deflate is enabled and one of the peer has queued data waiting for it to be compressed, framed and sent over the connection. Meanwhile, the other peer closes the connection. What should we do?

Currently the data processing keeps going as nothing happened and the callback of each enqueued send operation, if any, is invoked with an error when trying to write to the closed socket (after the 'close' event). This a waste of computational power. There is no reason to compress, frame, and try to send the data if we know beforehand that the socket is closed.

One way to work around the issue is to remove the enqueued send operations after invoking their callback with an error and before emitting the 'close' event.
The issue happens when a message is already being compressed so the queue should be cleared when compression completes.

Thoughts?

Reproducible in:

version: >=0.6.0
Node.js version(s): all
OS version(s): all

Steps to reproduce:

const crypto = require('crypto');
const WebSocket = require('ws');

const wss = new WebSocket.Server({
  perMessageDeflate: { threshold: 0 },
  port: 3000
}, () => {
  const ws = new WebSocket('ws://localhost:3000');

  ws.on('open', () => {
    setTimeout(() => ws.terminate(), 10);
  });
});

wss.on('connection', (ws) => {
  ws.on('close', () => console.log('close'));

  const data = crypto.randomBytes(1024);

  for (let i = 0; i < 1024; i++) {
    ws.send(data, (err) => {
      if (err) console.log(err);
    });
  }
});
lpinca added a commit that referenced this issue Nov 10, 2018
Do not invoke callbacks when clearing the send queue due to premature
socket closure.

Fixes #1226
lpinca added a commit that referenced this issue Nov 10, 2018
Do not invoke callbacks when clearing the send queue due to premature
socket closure.

Refs: #1464 (comment)

Fixes #1226
lpinca added a commit that referenced this issue Nov 10, 2018
Do not invoke callbacks when clearing the send queue due to premature
socket closure.

Refs: #1464 (comment)
Fixes #1226
lpinca added a commit that referenced this issue Nov 14, 2018
Do not invoke callbacks when clearing the send queue due to premature
socket closure.

Refs: #1464 (comment)
Fixes #1226
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant