Repository navigation
persistent-postgresql: read ResultError fields before freeing the result - #1645
Open
asselinpaul wants to merge 1 commit into
Open
asselinpaul wants to merge 1 commit into
asselinpaul wants to merge 1 commit into
Conversation
A ResultError from a failed column conversion holds its table OID and column name as lazy reads of the PGresult. withStmt' frees that result with unsafeFreeResult when the statement is released, which happens while the error is still propagating, so inspecting the error later read freed memory and could return wrong values or segfault. Force those fields before throwing, and add a regression test that decodes a BC date into a Day and inspects the error afterwards.
asselinpaul
force-pushed
the
postgresql-force-result-error-fields
branch
from
November 28, 2026 00:20
3a004e4 to
186d795
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We hit this in production at a company using persistent-postgresql: the process segfaulted in
PQftablewhile logging a failed row decode.Problem
When a column fails to convert (here a
dateholding a BC value, which postgresql-simple can't decode into aDay), postgresql-simple returns aResultErrorwhose table OID and column name are lazy reads of the libpq result:withStmt'throws that error frompullS, andcloseSfrees the result withLibPQ.unsafeFreeResultwhile the exception is still propagating. When the caller later shows or logs the error, those thunks callPQfname/PQftableon freed memory. Depending on the allocator, that gives an empty column name, a wrong OID, or a SIGSEGV that takes the process down before anything is logged. TheunsafeFreeResultdocs warn about this case: there must be "no references hiding inside an unevaluated thunk".Fix
Force
errSQLTableOidanderrSQLFieldbefore throwing, then usethrowIO. The other fields don't touch the result:errSQLTypecomes from the type cache, and the message is built from the value, whichgetvalue'copies. Exceptions that aren't aResultErrorare rethrown unchanged.Dropping the eager free would also fix this, but the GC can't see the size of libpq's allocation, so large results would stay alive much longer. Forcing two fields keeps the current memory behaviour.
Test
ResultErrorSpecstores0001-12-31 BCin a temporarydatecolumn, selects it so theDayconversion fails, catches theResultErrorafterrawSqlhas released the statement, and checks the column name and table OID. On master (Linux/glibc) it fails on every run: the column name comes back as"", and libpq printscolumn number 0 is out of range 0..-1. With the fix it passes, and so does the full persistent-postgresql suite.fourmoluon changed files