websockets/ws

Uncaught "zlib binding closed" errors

Closed this issue · 13 comments

I'm setting up a simple WebSockets server, and this error (apparently randomly) happened:

Error: zlib binding closed
    at DeflateRaw.Zlib._transform (zlib.js:485:15)
    at DeflateRaw.Transform._read (_stream_transform.js:167:10)
    at DeflateRaw.Transform._write (_stream_transform.js:155:12)
    at doWrite (_stream_writable.js:292:12)
    at clearBuffer (_stream_writable.js:396:7)
    at onwrite (_stream_writable.js:331:7)
    at WritableState.onwrite (_stream_writable.js:89:5)
    at afterTransform (_stream_transform.js:79:5)
    at TransformState.afterTransform (_stream_transform.js:54:12)
    at Zlib.callback (zlib.js:614:5)

It happened in a moment where the client was connecting and disconnecting continuously, so maybe it's caused by a race condition when the connection is closed abruptly. I wasn't able to reproduce it again. I had the PerMessageDeflate extension enabled (the WS server was configured as default).

It looks like it's the same issue as this: faye/permessage-deflate-node#1

Edit: I forgot, I'm using version 0.8.1 of ws.

Same Error here.

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: zlib binding closed
    at DeflateRaw.Zlib._transform (zlib.js:487:15)
    at DeflateRaw.Transform._read (_stream_transform.js:179:10)
    at DeflateRaw.Transform._write (_stream_transform.js:167:12)
    at doWrite (_stream_writable.js:301:12)
    at clearBuffer (_stream_writable.js:411:7)
    at onwrite (_stream_writable.js:344:7)
    at WritableState.onwrite (_stream_writable.js:105:5)
    at afterTransform (_stream_transform.js:99:5)
    at TransformState.afterTransform (_stream_transform.js:74:12)
    at Zlib.callback (zlib.js:611:5)

Server restarts about 1-2 times daily. 100 - 200 users online
NodeJS version v0.12.7
WS version 0.8.1 (even with latest commit)

Hi! It seems to be caused by the use of this._deflate.close but I don't understand why the error is not caught by the listener:

var PerMessageDeflate = require('./lib/PerMessageDeflate');
var extension = new PerMessageDeflate();

extension.accept([{}]);
extension.compress(new Buffer([1, 2, 3]), true, function(err, compressed) {
  console.log('compress %s', err);
  // compress Error: zlib binding closed
});
extension.cleanup();

The listener is removed there https://github.com/websockets/ws/blob/master/lib/PerMessageDeflate.js#L304, but there should not be any call to this._deflate.write or this._deflate.flush after that. Maybe some kind of race condition...

Hi! @DanReyLop @Niceman35 would you be able to test whether this PR #632 does actually fix the issue?

I have a (potentially) related problem, but in my case it's a lower-level assertion fail - see #633

@jancurn probably an older node version? Prior to this patch: nodejs/node-v0.x-archive#7775

@rauchg I don't think so: I ran a stress test once w/ compression that sent large amount of data back and forth on Node 5.x, and that same assertion fail (or maybe a different one, I can't quite remember) popped up every so often.

@SEAPUNK the assertion that crashes the process or an uncaught exception?

@rauchg The assertion that crashes the process. I'll do that test again to get the exact error, but not right now -- I'm investigating a different problem that for some reason is corrupting the data I'm trying to send over websockets.

@darrachequesne Sorry, but I've tried to reproduce the error in the current 0.8.1 version (which should produce the error) and I couldn't. That's the thing about race conditions, impossible to reproduce reliably... I guess you will have to fix blindly in this PR :(

Maybe @Niceman35 has more luck, if his server restarts 1-2 times a day because of this bug, having him run it with your patch for a few days can give us a more definite answer.

@rauchg The server was running Node version v4.2.3

My servers started to crash because of the reported "Error: zlib binding closed" error every ~5 minutes (have thousands of active users) as soon as i updated ws module from version 0.7.0 to 0.8.1 (latest version on npm as of today 30/12/2015).
The servers stopped crashing when I reverted ws module back to version 0.7.0
(Node 4.2.4 Linux)
Please fix x_x

If you are there already, please also fix related issue: (set error listener to catch error events BEFORE processing the first incoming user bytes)
#246 (comment)
I see this in my error logs (of version 0.8.1):
[WebSocket Error!] invalid compressed data 1007
[WebSocket Error!] invalid compressed data 1007
[WebSocket Error!] invalid compressed data 1007

Please fix x_x

Please make pull request.

Ralle commented

I am seeing this problem with version 0.8.1. I have Apache running as a reverse websocket proxy. While people are connected through the proxy to a 'ws' socket I get this problem every single time I restart Apache.

chat_1           | events.js:154
chat_1           |       throw er; // Unhandled 'error' event
chat_1           |       ^
chat_1           | 
chat_1           | Error: zlib binding closed
chat_1           |     at DeflateRaw.Zlib._transform (zlib.js:484:15)
chat_1           |     at DeflateRaw.Transform._read (_stream_transform.js:167:10)
chat_1           |     at DeflateRaw.Transform._write (_stream_transform.js:155:12)
chat_1           |     at doWrite (_stream_writable.js:301:12)
chat_1           |     at clearBuffer (_stream_writable.js:404:7)
chat_1           |     at onwrite (_stream_writable.js:340:7)
chat_1           |     at WritableState.onwrite (_stream_writable.js:89:5)
chat_1           |     at afterTransform (_stream_transform.js:79:3)
chat_1           |     at TransformState.afterTransform (_stream_transform.js:54:12)
chat_1           |     at Zlib.callback (zlib.js:613:5)

EDIT: I feel stupid now. Just realised the latest version is 2.1.0. Anyway, this still stands with version 0.8.1. I will try upgrading to 2.1.0 soon.