Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection.closed attribute is not set properly on localconnections #196

Closed
psycoteam opened this issue Mar 6, 2014 · 8 comments
Closed
Milestone

Comments

@psycoteam
Copy link

Originally submitted by: Psycopg website

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

@psycoteam
Copy link
Author

Originally submitted by: 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)'
>>> 

@dvarrazzo
Copy link
Member

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.

@psycoteam
Copy link
Author

Originally submitted by: Zaar Hai

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

@dvarrazzo
Copy link
Member

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

@psycoteam
Copy link
Author

Originally submitted by: Zaar Hai

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

@dvarrazzo
Copy link
Member

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.

@psycoteam
Copy link
Author

Originally submitted by: Zaar Hai

Verified: works for me at https://github.com/dvarrazzo/psycopg.git#d1e1243ba851641feb2920148a44de3b92542997

Waiting for 2.5.3.
Thanks!

@dvarrazzo
Copy link
Member

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

@psycoteam psycoteam added this to the psycopg2 2.5.3 milestone Aug 28, 2014
joulez pushed a commit to joulez/psycopg2 that referenced this issue Nov 13, 2014
This happens for Socket connections, not for TCP ones, where a result
containing an error is returned and correctly handled by pq_raise()

Closes ticket psycopg#196 but not psycopg#192: poll() still doesn't change the
connection closed.
gencer pushed a commit to gencer/psycopg2 that referenced this issue Aug 22, 2016
This happens for Socket connections, not for TCP ones, where a result
containing an error is returned and correctly handled by pq_raise()

Closes ticket psycopg#196 but not psycopg#192: poll() still doesn't change the
connection closed.
gencer pushed a commit to gencer/psycopg2 that referenced this issue Aug 22, 2016
This happens for Socket connections, not for TCP ones, where a result
containing an error is returned and correctly handled by pq_raise()

Closes ticket psycopg#196 but not psycopg#192: poll() still doesn't change the
connection closed.

Conflicts:

	NEWS
	psycopg/pqpath.c
This issue was closed.
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

No branches or pull requests

2 participants