Skip to content

Commit 6046a28

Browse files
committed
[fix] Do not prematurely remove the listener of the 'data' event
1 parent bf9b2ec commit 6046a28

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/websocket.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,6 @@ function socketOnClose () {
750750
const websocket = this[kWebSocket];
751751

752752
this.removeListener('close', socketOnClose);
753-
this.removeListener('data', socketOnData);
754753
this.removeListener('end', socketOnEnd);
755754
this[kWebSocket] = undefined;
756755

@@ -760,11 +759,16 @@ function socketOnClose () {
760759
// The close frame might not have been received or the `'end'` event emitted,
761760
// for example, if the socket was destroyed due to an error. Ensure that the
762761
// `receiver` stream is closed after writing any remaining buffered data to
763-
// it.
762+
// it. If the readable side of the socket is in flowing mode then there is no
763+
// buffered data as everything has been already written and `readable.read()`
764+
// will return `null`. If instead, the socket is paused, any possible buffered
765+
// data will be read as a single chunk and emitted synchronously in a single
766+
// `'data'` event.
764767
//
765768
websocket._socket.read();
766769
websocket._receiver.end();
767770

771+
this.removeListener('data', socketOnData);
768772
clearTimeout(websocket._closeTimer);
769773

770774
if (

0 commit comments

Comments
 (0)