This project is archived and is in readonly mode.

#196 ✓ resolved
Psycopg website

connection.closed attribute is not set properly on localconnections

Reported by Psycopg website | March 6th, 2014 @ 09:27 AM | in psycopg2 2.5.3 (closed)

Submitted by: haizaar

Scenario:
1. issue psycopg2.connect through local socket
2. restart database server
3. create cursor and execute query (and get an error)

Expected result:
connection.closed attribute will should be greater then 0

Actual result:
connection.closed attribute is 0

Steps to reproduce:

$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> conn = psycopg2.connect("dbname=tst host=/tmp")
>>> conn.closed
0
>>> # Issuse CTRL-Z
[1]+  Stopped                 python
$ service postgresql restart
 * Restarting PostgreSQL 9.1 database server
   ...done.
$ fg
python


>>> cur = conn.cursor()
>>> cur.execute("SELECT 1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

>>> conn.closed
0
>>> psycopg2.__version__
'2.5.2 (dt dec pq3 ext)'
>>>

Reproduced on:
python 2.7.5 / 2.6.8.
Postgresql 9.1 / 8.4
Ubuntu 13.10 64-bit / Centos 6.4 64-bit

Comments and changes to this ticket

  • Zaar Hai

    Zaar Hai March 6th, 2014 @ 09:31 AM

    Formatting did not go well through web form. The reproduction is below.

    $ python
    Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
    [GCC 4.8.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import psycopg2
    >>> conn = psycopg2.connect("dbname=tst host=/tmp")
    >>> conn.closed
    0
    >>> # Issuse CTRL-Z
    [1]+  Stopped                 python
    $ service postgresql restart
     * Restarting PostgreSQL 9.1 database server
       ...done.
    $ fg
    python
    
    
    >>> cur = conn.cursor()
    >>> cur.execute("SELECT 1")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.OperationalError: server closed the connection unexpectedly
            This probably means the server terminated abnormally
            before or while processing the request.
    
    >>> conn.closed    # <------------ Should be greater then zero!
    0
    >>> psycopg2.__version__
    '2.5.2 (dt dec pq3 ext)'
    >>>
    
  • Daniele Varrazzo

    Daniele Varrazzo March 6th, 2014 @ 11:11 AM

    • State changed from “new” to “open”

    Yup, thank you for the report: i'd noticed something similar in #192. Thank you for the test case, I'll try to fix it.

  • Zaar Hai

    Zaar Hai March 6th, 2014 @ 04:35 PM

    BTW, to emphasize, that if one connects through TCP and not local socket, then closed attribute is populated properly.

  • Daniele Varrazzo

    Daniele Varrazzo March 6th, 2014 @ 04:43 PM

    Ugh, thank you for the clarification, I wouldn't have expected such a difference.

  • Zaar Hai

    Zaar Hai March 6th, 2014 @ 05:07 PM

    Yeah, I've spotted it while working on Momoko - dead connection detection stopped functioning when one connects through local socket.

  • Daniele Varrazzo

    Daniele Varrazzo March 6th, 2014 @ 05:25 PM

    In debug mode, executing into a broken socket connection:

    In [4]: cur.execute('select 1')
    [19233] psyco_curs_execute: starting execution of new query
    [19233] curs_execute: pg connection at 0x2095ff0 OK
    [19233] pq_begin_locked: pgconn = 0x2095ff0, autocommit = 0, status = 1
    [19233] pq_execute_command_locked: pgconn = 0x2095ff0, query = BEGIN
    [19233] pq_execute_command_locked: PQexec returned NULL
    [19233] pq_complete_error: pgconn = 0x2095ff0, pgres = (nil), error = server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
    
    [19233] psyco_curs_execute: res = -1, pgres = (nil)
    

    In a tcp connection instead:

    [19233] psyco_curs_execute: starting execution of new query
    [19233] curs_execute: pg connection at 0x20e8120 OK
    [19233] pq_begin_locked: pgconn = 0x20e8120, autocommit = 0, status = 1
    [19233] pq_execute_command_locked: pgconn = 0x20e8120, query = BEGIN
    [19233] pq_execute_command_locked: result was not COMMAND_OK (7)
    [19233] pq_complete_error: pgconn = 0x20e8120, pgres = 0x2158e30, error = (null)
    [19233] pq_raise: PQresultErrorMessage: err=FATAL:  terminating connection due to administrator command
    SSL connection has been closed unexpectedly
    

    so this should be the branch point: PQexec returning NULL vs. a "not ok" result.

  • Zaar Hai
  • Daniele Varrazzo

    Daniele Varrazzo March 10th, 2014 @ 05:24 PM

    • Milestone set to psycopg2 2.5.3

    Thank you for testing: I'll try to clear the issues with poll too and pack up the release within a few days.

  • Daniele Varrazzo

    Daniele Varrazzo March 10th, 2014 @ 05:28 PM

    • State changed from “open” to “resolved”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

WARNING: the informations in this tracker are archived. Please submit new tickets or comments to the new tracker.


Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins

Tags

Referenced by

Pages