Ensure connection is usable after COPY stream is complete
Summary
Encountered this issue in a live environment.
Sometimes random queries were hanging indefinitely after using a COPY stream.
This PR likely fixes:
- https://github.com/porsager/postgres/issues/499
Cause
This was caused by receiving CopyData and CopyDone messages within the same chunk.
If CopyData triggers high water mark, handler pauses the socket, but still continues to process remaining messages, that were already received.
If CopyDone is handled before stream managed to process buffered messages, the connection becomes unusable - the socket is paused. Any query using that connection will hang indefinitely (with response data in the socket read buffer).
Fix
Make sure socket is flowing after COPY stream is done.
The test is a little complicated, but I couldn't find a simpler way to reliably reproduce the issue (now without the fix the test will timeout every time).
Nice work! Looks good!
You don't need the changes in the generated folders (cf, cjs, deno) so could you discard those changes?
Also perhaps rebase on your latest PR that fixed CI
I updated the PR. But looks like more work is needed here. Locally I was testing with Node22+Postgres 16. And that works in CI too. But most versions combos / deno fail on that test. Specifically only Node22 seems to work with all versions of PG. All other versions of node fail.
Feel free to play with it. But if not, I'll try to spend some time this weekend to find a way to make that test work.
@porsager PR is updated and CI is green.