-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
readable
event not emitted after net.Socket
reconnects
#25969
Comments
Stopped working in v10.10.0. Calling |
Reverting 53fb7af1b2 makes cc: @mcollina |
@morkai btw, |
@lpinca even if called without any arguments?
|
Looking at the code it seems that that sentence is correct. However a new sentence has been added
See #25375 for context. |
This is does not look like a bug in const { Readable } = require('stream')
const r = new Readable({
read() {
}
})
r.push(Buffer.from('hello'))
r.push(Buffer.from(' '))
r.push(Buffer.from('world'))
r.push(null)
r.on('readable', () => {
console.log('readable')
let chunk
while ((chunk = r.read()) !== null) {
console.log('chunk', chunk.toString())
}
}) I think this is something specific to how |
@mcollina My guess would be that |
That might be the case. |
Before v10.10, Since v10.10, This makes See: https://gist.github.com/morkai/a6e8d696f11aa2fb903a369c775e8c76#file-log-txt |
It seems there is also a behavior change between Node.js 8 and Node.js 10, probably wanted but I'm not sure. Consider the following example: 'use strict';
const { Readable } = require('stream');
const buf = Buffer.alloc(8192);
const readable = new Readable({
read() {
this.push(buf);
}
});
readable.on('readable', function() {
const data = readable.read();
console.log(data.length);
}); On Node.js 8, it creates (as expected I would say) an endless loop. On Node.js 10 it exits after a few reads. Not sure what happens and if it is expected but at first glance it seems a race condition caused by |
Added confirmed-bug label because I think |
If the original issue is fixed then yes. |
@morkai: Is this still an issue? |
Yes. The issue is still present in v12.13.1 and v14.1.0. |
@morkai: Is it possible for you to instead of re-using the socket, simply create a new one? @mcollina: I think we briefly discussed at some point that the |
Fixing would be good. Otherwise deprecating is also an option. It seems this issue is not getting enough attention - or folks willing to work on it. |
I fixed it in the library that was affected by this issue by calling the Instead of: socket.on('connect', () => {
console.log('connected!');
});
socket.on('readable', () => {
while (true) {
const data = socket.read();
if (!data) break;
console.log('data', data);
}
}); this: socket.on('connect', () => {
console.log('connected!');
onReadable();
});
socket.on('readable', onReadable);
function onReadable()
{
while (true) {
const data = socket.read();
if (!data) break;
console.log('data', data);
}
} |
I think we can add that to core. |
fix readable event not emitted after reconnect
fix readable event not emitted after reconnect Fixes: nodejs#25969
fix readable event not emitted after reconnect Fixes: nodejs#25969
If
net.Socket
loses connection (close
is emitted) and the same socket instance is used to reconnect to the same server, no morereadable
events are emitted (data
events are still emitted).Doesn't work in v10.14/v10.15.1. Works in v8.15.0.
Repro: https://gist.github.com/morkai/fa175bd0104443e6142f3d0e22805653
server.js
client.js
client#readable
client#readable
linesdata
event handler and comment thereadable
handler inclient.js
client#data
client#data
linesThe text was updated successfully, but these errors were encountered: