-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
node 16.x does not raise error on write when server closes connection #38704
Comments
@nodejs/streams ? |
@nodejs/net |
This behavior is correct and wanted, and it is the combination of a few PRs that landed in v15. What is happening is:
In your client example, you are not waiting 1 second before sending the Here is a complete example: let os = require('os')
let net = require('net')
let crypto = require('crypto')
let socketPath = `${os.tmpdir()}/${crypto.randomBytes(12).toString('hex')}.sock`
let server = net.createServer(socket => {
let buffer = Buffer.alloc(0)
socket.on('data', chunk => {
console.log('chunk', chunk.toString())
if(chunk.toString('utf8') === 'destroy') {
console.log('server destroy')
socket.destroy()
}
})
})
server.on('listening', () => {
// client closes
const socket1 = net.connect({ path: socketPath })
socket1.on('error', (e) => {
console.log('socket1:' + e.toString())
})
socket1.end()
setTimeout(function () {
socket1.write('hello', function (err) {
console.log('hello written', err)
})
}, 1000)
// server closes
const socket2 = net.connect({ path: socketPath })
socket2.on('error', (e) => {
console.log('socket2:' + e.toString())
})
socket2.write('destroy')
setTimeout(() => {
socket2.write('something', function (err) {
console.log('something written', err)
})
}, 1000)
socket2.on('end', () => {
console.log('socket2 ended')
})
socket2.on('close', () => {
console.log('socket2 closed')
})
})
server.listen(socketPath) |
@mcollina I understand the reasoning and it is more consistent, the documentation could be a bit more clear that the callback for socket.write callback will returns an error as this quite important to avoid silently loosing the write. |
Agreed. https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback looks quite confusing in this regard. |
Would you like to send a pull request? |
Hello @mcollina does this still need a PR, I would love to take a shot at this |
Yes it does, it's a doc update. |
could you point me to the doc file. thank you |
https://github.com/nodejs/node/blob/master/doc/api/stream.md |
Hello @mcollina |
@tlbdk what did you find unclear in the current docs? What would you change? |
@mcollina sir, I am new to open source, can you please guide me on what this issue is about, so that I can try to contribute, Thanks! |
Check out https://www.nodetodo.org/ This issue is probably too hard for a newcomer, I removed the "good first issue" tag. |
The doc change requires somebody who knows exactly how streams behave in 16.x. Adding a clarification about the behavior discussed in this thread would still not clarify the existing documentation, which states:
Based on what I understand from this thread we are saying that it's expected that once the Simply adding this explanation would still not clarify under which circumstances:
If somebody could clarify what are those circumstances fixing the docs will be easier. |
After |
@mcollina apologies for being pedantic but this still doesn't clarify it fully, also suggesting that getting this right is not straightforward. Quoting you:
The first sentence somewhat contradicts the second. If the callback is always called with an error object if one exists, how can there not be any stream-wide errors anymore after Perhaps the second sentence means more specifically that If that is the case, do we agree that this contradicts the current docs which say:
and which need to be fixed by removing that sentence and replacing it with a rephrase of what you wrote above? Also, the docs say right after the previous quote:
That would then not be completely accurate, because in order to reliably detect errors you would have both to listen to the |
- replace _*may or may not* be called_ with _will be called_ because the callback is always called - remove _To reliably detect write errors, add a listener for the `'error'` event_ because the `error` event will NOT be emitted if a write occurs after the stream has been closed PR-URL: #38959 Fixes: #38704 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- replace _*may or may not* be called_ with _will be called_ because the callback is always called - remove _To reliably detect write errors, add a listener for the `'error'` event_ because the `error` event will NOT be emitted if a write occurs after the stream has been closed PR-URL: #38959 Fixes: #38704 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Node version and platform
$ node -v v16.0.0 $ uname -a Darwin Troelss-Mac-mini.local 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:41 PDT 2021; root:xnu-7195.101.2~1/RELEASE_ARM64_T8101 arm64
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Every time.
What is the expected behavior?
Node 12.x:
Node 14.x:
What do you see instead?
Node 16.x:
Additional information
The text was updated successfully, but these errors were encountered: