Skip to content

persistent-postgresql: read ResultError fields before freeing the result - #1645

Open
asselinpaul wants to merge 1 commit into
yesodweb:masterfrom
asselinpaul:postgresql-force-result-error-fields
Open

asselinpaul wants to merge 1 commit into
yesodweb:masterfrom
asselinpaul:postgresql-force-result-error-fields

Conversation

@asselinpaul

Copy link
Copy Markdown

We hit this in production at a company using persistent-postgresql: the process segfaulted in PQftable while logging a failed row decode.

Problem

When a column fails to convert (here a date holding a BC value, which postgresql-simple can't decode into a Day), postgresql-simple returns a ResultError whose table OID and column name are lazy reads of the libpq result:

-- postgresql-simple, Database.PostgreSQL.Simple.FromField
name Field{..} = unsafeDupablePerformIO (PQ.fname result column)
tableOid Field{..} = toMaybeOid (unsafeDupablePerformIO (PQ.ftable result column))

withStmt' throws that error from pullS, and closeS frees the result with LibPQ.unsafeFreeResult while the exception is still propagating. When the caller later shows or logs the error, those thunks call PQfname/PQftable on 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. The unsafeFreeResult docs warn about this case: there must be "no references hiding inside an unevaluated thunk".

Fix

Force errSQLTableOid and errSQLField before throwing, then use throwIO. The other fields don't touch the result: errSQLType comes from the type cache, and the message is built from the value, which getvalue' copies. Exceptions that aren't a ResultError are 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

ResultErrorSpec stores 0001-12-31 BC in a temporary date column, selects it so the Day conversion fails, catches the ResultError after rawSql has 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 prints column number 0 is out of range 0..-1. With the fix it passes, and so does the full persistent-postgresql suite.

  • Ran fourmolu on changed files
  • Updated the ChangeLog and bumped persistent-postgresql to 2.14.3.1

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
asselinpaul force-pushed the postgresql-force-result-error-fields branch from 3a004e4 to 186d795 Compare November 28, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant