Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

connection.closed attribute is not set properly on localconnections

#196

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

Reported by Psycopg website · March 6th, 2014 @ 09:27 AM

State: resolved
Milestone: psycopg2 2.5.3
Assigned to: nobody

Activity

  1. Zaar Hai
    Zaar Hai

    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)'
    >>>
    

    March 6th, 2014 @ 09:31 AM

  2. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to open

    Yup, thank you for the report: i'd noticed something similar in #192 enhancement: connection.closed attribute to actually become a property that d.... Thank you for the test case, I'll try to fix it.

    March 6th, 2014 @ 11:11 AM

  3. Zaar Hai
    Zaar Hai

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

    March 6th, 2014 @ 04:35 PM

  4. Daniele Varrazzo
    Daniele Varrazzo

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

    March 6th, 2014 @ 04:43 PM

  5. Zaar Hai
    Zaar Hai

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

    March 6th, 2014 @ 05:07 PM

  6. Daniele Varrazzo
    Daniele Varrazzo

    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.

    March 6th, 2014 @ 05:25 PM

  7. Zaar Hai
  8. Daniele Varrazzo
    Daniele Varrazzo
    • 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.

    March 10th, 2014 @ 05:24 PM

  9. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from open to resolved

    March 10th, 2014 @ 05:28 PM