-
Notifications
You must be signed in to change notification settings - Fork 336
fix: end() hangs forever after ECONNRESET error #1142
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
base: master
Are you sure you want to change the base?
Conversation
- Clear query variable in errored() after rejecting query - Clear query variable in closed() when hadError is true - Add test to verify end() completes after connection errors Fixes porsager#1130
| return reconnect() | ||
|
|
||
| !hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket)) | ||
| if (hadError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is slightly weird to me. If (we had an error or sent.length isn't 0) and query is empty then call error which will clean the query regardless right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't your code just mean that regardless if we had an error or not we should just clear the queue (which error function does) and show econreset?
| if (hadError) { | ||
| // Clear query when connection closes with error (e.g., ECONNRESET) | ||
| query && (queryError(query, Errors.connection('CONNECTION_CLOSED', options, socket)), query = null) | ||
| while (sent.length) | ||
| queryError(sent.shift(), Errors.connection('CONNECTION_CLOSED', options, socket)) | ||
| } else { | ||
| (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket)) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (hadError) { | |
| // Clear query when connection closes with error (e.g., ECONNRESET) | |
| query && (queryError(query, Errors.connection('CONNECTION_CLOSED', options, socket)), query = null) | |
| while (sent.length) | |
| queryError(sent.shift(), Errors.connection('CONNECTION_CLOSED', options, socket)) | |
| } else { | |
| (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket)) | |
| } | |
| (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket)) //and query=null somewhere |
Problem
When a query fails with
ECONNRESET(or any connection error), callingpg.end()hangs forever. This happens because:queryvariable in the connection is never clearedReadyForQueryclearsquery = null, but connection errors never reach that handlerend()is called, it checks!queryto determine if it can terminate immediatelyquerystill references the rejected query object, the condition fails andend()creates a promise waiting forendedto be calledendedis only called interminate(), which is only called when!queryis true - creating a deadlockSolution
I've made two changes to ensure the
queryvariable is properly cleared when connection errors occur:errored()function: Clearquery = nullafter rejecting the queryclosed()function: WhenhadErroris true, clear the query before closing (sinceerrored()might not always be called in all error paths)Changes
src/connection.js:errored()to clearqueryafterqueryError()closed()to clearquerywhenhadErroris truetests/index.js: Added test'end() completes after ECONNRESET error'to verify the fixTesting
The new test creates a real PostgreSQL connection, terminates the backend during a query execution, and verifies that
end()completes quickly (within 1 second) instead of hanging forever.Notes
I'm not entirely sure if this is the best approach - I spent a significant amount of time understanding the codebase and the problem. The connection lifecycle and query state management is quite complex, and I wanted to make sure I understood the flow before making changes. I also received help from AI to better understand the problem and explore the codebase, which was invaluable in identifying where the
queryvariable needed to be cleared.I'm open to feedback and alternative approaches if there's a better way to handle this!
Fixes #1130