| From: | Robert Haas <robertmhaas(at)gmail(dot)com> |
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | marcos(dot)castedo(at)anachronics(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Re: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails |
| Date: | 2026-11-16 19:44:21 |
| Message-ID: | CA+TgmoZvBUgj=1QYTFzq_yCboVdw7U2OK3p-rmcEwzKzpZ1DDg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Mon, Nov 16, 2026 at 12:53 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> The immediate cause in this example seems to be
> (1) PyBytes_FromStringAndSize returns NULL after a malloc failure
> (2) We blindly pass that NULL to PyDict_SetItemString
> (3) PyDict_SetItemString doesn't cope with that and dumps core.
>
> It looks to me like plpy_typeio.c is just full of bugs like this,
> because it doesn't have any coherent strategy for detecting or
> dealing with failures. There are a few places that check for
> NULL results and throw PLy_elog(ERROR), but not many. I think
> we'd first want to decide whether to check immediately after
> each call to a Python function, or try to centralize it a bit.
Here's what my LLM says:
The problem here is that PL/Python's input conversion functions follow
the Python convention of returning NULL, with an exception set, when
Python can't build the object, and none of the backend code that
calls them checks. Here the copy of the bytea fails with MemoryError,
and PLyDict_FromTuple stores the NULL straight into the dictionary.
d0aa965c0a (Peter Eisentraut, 2017-10-31) stated the rule: code called
from Python returns NULL, and code called from PostgreSQL raises an
error. The input conversions are only ever called from backend code,
which was supposed to turn the NULL into an error and never does.
Marcos is right that row-type arguments and TD["new"] go through the
same code, and they crash too, as does an array element. A scalar
argument is worse: argument setup treats a NULL result as an SQL NULL,
so the function quietly runs with None and returns a wrong answer.
The attached patch makes the two entry points, PLy_input_convert and
PLy_input_from_tuple, throw an error on a NULL result, as the
numeric-to-Decimal conversion already does, and makes argument setup
use None only for an SQL NULL. Every case then fails with an ordinary
ERROR and the session survives. A plpython3u function that lowers
RLIMIT_AS on its own backend reproduces this without a restart; that's
the attached setup.sql.
--
Robert Haas
Databricks
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Fix-PL-Python-crashes-when-a-value-can-t-be-converte.patch | application/octet-stream | 5.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Robert Haas | 2026-11-16 20:11:04 | Re: BUG #19903: ALTER COLUMN TYPE: "unexpected object depending on column" internal error via partition key |
| Previous Message | Daniel Gustafsson | 2026-11-16 19:33:19 | Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) |