Skip to content

Fix deallocation of failed digested prepares - #2642

Open
AshSgDe29071999 wants to merge 1 commit into
jackc:masterfrom
AshSgDe29071999:fix/failed-describe-deallocate-psname
Open

Fix deallocation of failed digested prepares#2642
AshSgDe29071999 wants to merge 1 commit into
jackc:masterfrom
AshSgDe29071999:fix/failed-describe-deallocate-psname

Conversation

@AshSgDe29071999

Copy link
Copy Markdown

Fixes #2640

database/sql / GORM with PrepareStmt: true call Prepare(ctx, sql, sql). In that path the wire name is stmt_<digest> while the client cache key is the SQL text.

On a Describe-phase PrepareError, failedDescribeStatement stored the cache key. The next Prepare on that connection then called Deallocate with the SQL text. A failed prepare is never in preparedStatements, so Deallocate sent Close for a name that does not exist, the leaked stmt_<digest> stayed on the server, and a retry of the same SQL returned 42P05 for the rest of the connection's life. Cleanup errors also surfaced on whatever query next borrowed the pooled connection.

Store psName (the identifier actually sent in Parse) so the follow-up Close targets the real server statement. The named-prepare path is unchanged because psName == psKey there.

The new test is the digest (name == sql) variant of TestPrepareHandlesTimeoutBetweenParseAndDescribe.

When Prepare is called with name == sql (the stdlib path), the server-side
statement is stmt_<digest> but a Describe-phase failure stored the SQL text.
The next Prepare then Close'd a name that did not exist, leaked the digest
statement, and retried with 42P05.

Store the Parse name so cleanup targets the real server statement.

See jackc#2640
@eliranbz

eliranbz commented Aug 28, 2026

Copy link
Copy Markdown

Thanks for the PR, but I could have also let CC fix this in a second - I want to get feedback and see if this issue goes deeper or is this an issue on my implementation rather than a bug.

@jackc this seem to solve the issue, but would be happy to hear your thoughts on the issue. i have created #2644 for this. this PR stops pointless cleanups from being scheduled at all, with tests that prove both the original issue and this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants