Thread: BUG #5835: PL/Python crashes

BUG #5835: PL/Python crashes

From
"Julien Demoor"
Date:
The following bug has been logged online:

Bug reference:      5835
Logged by:          Julien Demoor
Email address:      jdemoor@gmail.com
PostgreSQL version: 9.0.2
Operating system:   Ubuntu Linux
Description:        PL/Python crashes
Details:

Description of the system on which the problem occurs:
======================================================

$ psql --version
psql (PostgreSQL) 9.0.2

$ uname -a
Linux <hostname> 2.6.32-27-generic #49-Ubuntu SMP Wed Dec 1 23:52:12 UTC
2010 i686 GNU/Linux

PostgreSQL packages obtained through
https://launchpad.net/~pitti/+archive/postgresql and installed with Ubuntu's
package manager.

Reproduced on two machines, both running the same OS and Postgres version.
One of the two runs the stock postgresql.conf.

Steps to reproduce the problem:
===============================

In psql, create a database and install PL/Python:
CREATE DATABASE test2;
\c test2
CREATE LANGUAGE plpython2u;

Run the following in psql:

BEGIN;
CREATE OR REPLACE FUNCTION test_none_3(a numeric[]) RETURNS numeric(12,4) AS
$$
    return 1;
$$ LANGUAGE plpython2u;
SELECT test_none_3('{NULL, NULL, NULL}'::numeric[]) FROM
generate_series(1,244) ORDER BY generate_series DESC LIMIT 1;
ROLLBACK;

Transcript of what happens:
===========================
Full transcript:

$ psql -d test2
psql (9.0.2)
Type "help" for help.

test2=# BEGIN;
BEGIN
test2=# CREATE OR REPLACE FUNCTION test_none_3(a numeric[]) RETURNS
numeric(12,4) AS $$
test2$#     return 1;
test2$# $$ LANGUAGE plpython2u;
SELCREATE FUNCTION
test2=# SELECT test_none_3('{NULL, NULL, NULL}'::numeric[]) FROM
generate_series(1,244) ORDER BY generate_series DESC LIMIT 1;
BACK;
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!> ROLLBACK;
You are currently not connected to a database.
!> ^D\q

In the server logs:
===================
Fatal Python error: deallocating None
2011-01-13 14:20:10 CET LOG:  server process (PID 27747) was terminated by
signal 6: Aborted
2011-01-13 14:20:10 CET LOG:  terminating any other active server processes
2011-01-13 14:20:10 CET WARNING:  terminating connection because of crash of
another server process
2011-01-13 14:20:10 CET DETAIL:  The postmaster has commanded this server
process to roll back the current transaction and exit, because another
server process exited abnormally and possibly corrupted shared memory.
2011-01-13 14:20:10 CET HINT:  In a moment you should be able to reconnect
to the database and repeat your command.
2011-01-13 14:20:10 CET FATAL:  the database system is in recovery mode

More tests to reproduce:
========================
Changing the constant array and the second parameter given to
generate_series() we get:

-- {NULL, NULL, NULL}: Crashes at 244 and more, but not less
-- {NULL, 12, NULL}: Crashes 366 and more, but not less
-- {NULL, NULL}: Crashes 366 and more, but not less
-- {NULL, 12}: Crashes at 730 and more, but not less
-- {NULL}: Crashes at 730 and more, but not less
-- {}: Doesn't crash even at 1'000'000
-- {12}: Doesn't crash even at 1'000'000

Re: BUG #5835: PL/Python crashes

From
Alex Hunsaker
Date:
On Thu, Jan 13, 2011 at 06:57, Julien Demoor <jdemoor@gmail.com> wrote:
>
> The following bug has been logged online:
>
> Bug reference:      5835
> Logged by:          Julien Demoor
> Email address:      jdemoor@gmail.com
> PostgreSQL version: 9.0.2
> Operating system:   Ubuntu Linux
> Description:        PL/Python crashes
> Details:

> BEGIN;
> CREATE OR REPLACE FUNCTION test_none_3(a numeric[]) RETURNS numeric(12,4) AS
> $$
>    return 1;
> $$ LANGUAGE plpython2u;
> SELECT test_none_3('{NULL, NULL, NULL}'::numeric[]) FROM
> generate_series(1,244) ORDER BY generate_series DESC LIMIT 1;
> ROLLBACK;

It looks like we are not incrementing a reference count for NULL
entries in an array.  The attached fixes it for me.  I did look for
other places where we might have missed this and did not find any.  (
the PyDict_SetItemString() that use Py_None look fishy, but apparently
work ).

Can you see if it fixes it for you?

Attachment

Re: BUG #5835: PL/Python crashes

From
"Julien Demoor"
Date:
I just tried and it works. Thank you very much.

-----Message d'origine-----
De : Alex Hunsaker [mailto:badalex@gmail.com]=20
Envoy=C3=A9 : vendredi 14 janvier 2011 07:33
=C3=80 : Julien Demoor
Cc : pgsql-bugs@postgresql.org
Objet : Re: [BUGS] BUG #5835: PL/Python crashes

On Thu, Jan 13, 2011 at 06:57, Julien Demoor <jdemoor@gmail.com> wrote:
>
> The following bug has been logged online:
>
> Bug reference:      5835
> Logged by:          Julien Demoor
> Email address:      jdemoor@gmail.com
> PostgreSQL version: 9.0.2
> Operating system:   Ubuntu Linux
> Description:        PL/Python crashes
> Details:

> BEGIN;
> CREATE OR REPLACE FUNCTION test_none_3(a numeric[]) RETURNS=20
> numeric(12,4) AS $$
>    return 1;
> $$ LANGUAGE plpython2u;
> SELECT test_none_3('{NULL, NULL, NULL}'::numeric[]) FROM
> generate_series(1,244) ORDER BY generate_series DESC LIMIT 1;=20
> ROLLBACK;

It looks like we are not incrementing a reference count for NULL entries in=
 an array.  The attached fixes it for me.  I did look for other places wher=
e we might have missed this and did not find any.  ( the PyDict_SetItemStri=
ng() that use Py_None look fishy, but apparently work ).

Can you see if it fixes it for you?

Re: BUG #5835: PL/Python crashes

From
Alvaro Herrera
Date:
Excerpts from Alex Hunsaker's message of vie ene 14 03:33:17 -0300 2011:
> On Thu, Jan 13, 2011 at 06:57, Julien Demoor <jdemoor@gmail.com> wrote:

> It looks like we are not incrementing a reference count for NULL
> entries in an array.  The attached fixes it for me.  I did look for
> other places where we might have missed this and did not find any.  (
> the PyDict_SetItemString() that use Py_None look fishy, but apparently
> work ).

Thanks, applied to 9.0 and HEAD.

--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support