Repository navigation
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.
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.
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.
| 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 |
|
Independent confirmation of this from a separate investigation, plus one observation about the second hunk and one about a sibling case this does not reach. The We are shipping exactly the one-line change: query && (queryError(query, err), query = null)pinned as a package patch, and running it in production. Reproduction is a real backend killed mid-query with The We also checked the other paths through A sibling case survives this fix. The reverse interleaving — That is a separate fix — presumably |
|
Correcting my earlier comment: this hunk has a silent-wrong-answer regression, and we no longer ship it in this form. I said the
Stock keeps the rejected query pinned in the Minimal reproduction — one connection, const a = sql`select ${undefined}`.catch(e => `rejected(${e.code})`)
const b = sql`select 42 as value`The first statement is an application bug that reports itself through its own rejection. The second is innocent and gets a wrong answer with no error anywhere. On a shared pool under a request path that is worse than the hang the change fixes, because the hang is at least visible. What works instead: leave We have this pinned as a package patch and covered by two container tests, and each one fails on the placement the other requires — which is the part that convinced me the placement is load-bearing rather than a matter of taste. One caveat if you take this route: This does not change anything about the diagnosis in the PR description, which is correct and was ahead of everyone else — only where the assignment belongs. |
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