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

ERR_STREAM_WRITE_AFTER_END #121

Closed
joshxyzhimself opened this issue Nov 10, 2020 · 13 comments
Closed

ERR_STREAM_WRITE_AFTER_END #121

joshxyzhimself opened this issue Nov 10, 2020 · 13 comments
Labels
bug Something isn't working

Comments

@joshxyzhimself
Copy link

Just encountered this while testing, will close for now as I'm not sure what caused it and I don't have a repro yet.

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at Socket.Writable.write (_stream_writable.js:292:11)
    at Immediate.write [as _onImmediate] (/home/project/node_modules/postgres/lib/connection.js:331:12)
    at processImmediate (internal/timers.js:456:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
@joshxyzhimself
Copy link
Author

2nd time encountering it, same error.

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at Socket.Writable.write (_stream_writable.js:292:11)
    at Immediate.write [as _onImmediate] (/home/project/node_modules/postgres/lib/connection.js:331:12)
    at processImmediate (internal/timers.js:456:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

@porsager
Copy link
Owner

Hey @joshxyzhimself

I think it's fine keeping the issue open even though you don't have a repro. Maybe others can chime in with ideas / a repro.

Is this with v1?

@joshxyzhimself
Copy link
Author

joshxyzhimself commented Nov 10, 2020

Hi @porsager, thanks just reopened it. Currently using 2.0.0-beta.2, also using PostgreSQL 12.2 (Ubuntu 12.2-4)

@joshxyzhimself
Copy link
Author

joshxyzhimself commented Jan 20, 2021

Encountered this again locally with ^2.0.0-beta.2, been doing some initialization script (e.g. create x tables, insert y rows), noticed this one's a bit different than the errors above. It's not consistent, it mostly works, but this one has shown up 3 times today already and I'm not even using third-party stuff like nodemon

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at Socket.Writable.write (_stream_writable.js:292:11)
    at write (/home/project/node_modules/postgres/lib/connection.js:331:12)
    at Object.write (/home/project/node_modules/postgres/lib/connection.js:317:16)
    at send (/home/project/node_modules/postgres/lib/connection.js:136:18)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}

@joshxyzhimself
Copy link
Author

joshxyzhimself commented Feb 22, 2021

I'm starting to think this one is partly caused by me using nodemon and not using prexit

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at Socket.Writable.write (_stream_writable.js:292:11)
    at Immediate.write [as _onImmediate] (/home/project/node_modules/postgres/lib/connection.js:331:12)
    at processImmediate (internal/timers.js:456:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at Socket.Writable.write (_stream_writable.js:292:11)
    at Immediate.write [as _onImmediate] (/home/project/node_modules/postgres/lib/connection.js:331:12)
    at processImmediate (internal/timers.js:456:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
}
{
  "code": "ERR_STREAM_WRITE_AFTER_END"
}

@porsager porsager added the bug Something isn't working label Mar 23, 2021
@naumf
Copy link

naumf commented May 12, 2021

Don't have a repro too and not sure if it helps, but I got that error twice since setting the idle_timeout to 2 seconds.
Previously it was set to 8 seconds and didn't get that error.
Now I set it to 0, the default value and hopefully will see in the coming days if this is related to idle_timeout or not.

@joshxyzhimself
Copy link
Author

Hey @naumf, just noticed that my idle_timeout value is also 2. Maybe worth exploring.

@naumf
Copy link

naumf commented Jun 16, 2021

Hey @naumf, just noticed that my idle_timeout value is also 2. Maybe worth exploring.

In my case, the error didn't show up since setting the idle_timeout to 0.

@joshxyzhimself
Copy link
Author

Hey @naumf, just noticed that my idle_timeout value is also 2. Maybe worth exploring.

In my case, the error didn't show up since setting the idle_timeout to 0.

I'll try that. The error message also implies that we're writing on the socket while it's ending, could be the case.

@naumf
Copy link

naumf commented Jun 17, 2021

I'll try that. The error message also implies that we're writing on the socket while it's ending, could be the case.

Sure thing. Given the difficulty of reproducing the bug, this was the easier "fix" (workaround). It might not work for everyone though, as someone might need the idle_timeout to be enabled.

@joshxyzhimself
Copy link
Author

joshxyzhimself commented Jun 17, 2021

Note: links below are at v2.0.0-beta.2 snapshot, which was the version we were at upon encountering the errors above; nodejs links below are at v16.3.0

Usage of idle_timeout here calls the socket.end method.

timer = setTimeout(socket.end, idle_timeout * 1000)

According to nodejs docs[1], calling the stream.write() method after calling stream.end() will raise an error.

It seems that setImmediate(write) below

next === null && (next = setImmediate(write))

calls the socket.write(buffer) line below

socket.write(buffer)

which raises the error below

https://github.com/nodejs/node/blob/e65a25c077fb8b45fc8fe1fea5a7cc20d27aa744/lib/internal/streams/writable.js#L319

which leads to

https://github.com/nodejs/node/blob/e65a25c077fb8b45fc8fe1fea5a7cc20d27aa744/lib/internal/streams/writable.js#L630-L632

so, maybe check if the socket is ending already before we call any socket.write?

References

@naumf
Copy link

naumf commented Jun 17, 2021

so, maybe check if the socket is ending already before we call any socket.write?

That would prevent the error from showing up, but I think we need to know what data is lost, should we queue it and retry writing to the socket once re-connected etc.

@joshxyzhimself
Copy link
Author

it seems like setting idle_timeout to 0 solved it on my end, havent seen that error for a long time. we're also using v2.0.0-beta.6 now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants