{"action":"created","issue":{"url":"https://api.github.com/repos/FSX/momoko/issues/115","labels_url":"https://api.github.com/repos/FSX/momoko/issues/115/labels{/name}","comments_url":"https://api.github.com/repos/FSX/momoko/issues/115/comments","events_url":"https://api.github.com/repos/FSX/momoko/issues/115/events","html_url":"https://github.com/FSX/momoko/pull/115","id":100837497,"number":115,"title":"Fixed error handling when getting a connection.","user":{"login":"jchumnanvech","id":121485,"avatar_url":"https://avatars.githubusercontent.com/u/121485?v=3","gravatar_id":"","url":"https://api.github.com/users/jchumnanvech","html_url":"https://github.com/jchumnanvech","followers_url":"https://api.github.com/users/jchumnanvech/followers","following_url":"https://api.github.com/users/jchumnanvech/following{/other_user}","gists_url":"https://api.github.com/users/jchumnanvech/gists{/gist_id}","starred_url":"https://api.github.com/users/jchumnanvech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jchumnanvech/subscriptions","organizations_url":"https://api.github.com/users/jchumnanvech/orgs","repos_url":"https://api.github.com/users/jchumnanvech/repos","events_url":"https://api.github.com/users/jchumnanvech/events{/privacy}","received_events_url":"https://api.github.com/users/jchumnanvech/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-08-13T18:31:58Z","updated_at":"2015-08-26T11:39:04Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/FSX/momoko/pulls/115","html_url":"https://github.com/FSX/momoko/pull/115","diff_url":"https://github.com/FSX/momoko/pull/115.diff","patch_url":"https://github.com/FSX/momoko/pull/115.patch"},"body":"Right now we are encountering a bug when someone resets the postgres-db service. The connections become stale. When psycopg2 Error is thrown set_exc_info with error throws another exception which causes the connection to not be put back in the connection pool (line 560 is not executed because line 559 blows up). Its in the the tornado concurrent exception traceback logger. \r\n\r\nIt is trying to pass in the error into             \r\nself.formatted_tb = traceback.format_exception(*exc_info) and exc_info is only 1 parameter and it excepts 3\r\nThis is in concurrent.py line 113\r\n\r\nChanging it to set exception seems to make it work."},"comment":{"url":"https://api.github.com/repos/FSX/momoko/issues/comments/134958107","html_url":"https://github.com/FSX/momoko/pull/115#issuecomment-134958107","issue_url":"https://api.github.com/repos/FSX/momoko/issues/115","id":134958107,"user":{"login":"haizaar","id":58201,"avatar_url":"https://avatars.githubusercontent.com/u/58201?v=3","gravatar_id":"","url":"https://api.github.com/users/haizaar","html_url":"https://github.com/haizaar","followers_url":"https://api.github.com/users/haizaar/followers","following_url":"https://api.github.com/users/haizaar/following{/other_user}","gists_url":"https://api.github.com/users/haizaar/gists{/gist_id}","starred_url":"https://api.github.com/users/haizaar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haizaar/subscriptions","organizations_url":"https://api.github.com/users/haizaar/orgs","repos_url":"https://api.github.com/users/haizaar/repos","events_url":"https://api.github.com/users/haizaar/events{/privacy}","received_events_url":"https://api.github.com/users/haizaar/received_events","type":"User","site_admin":false},"created_at":"2015-08-26T11:39:04Z","updated_at":"2015-08-26T11:39:04Z","body":"With async code, shutting socket down for writes, does not set `conn.closed`. But shutting it for reads, does the trick. However `DatabaseError` except of `OperationalError` is thrown. So need to catch both of them for retrials:\r\n\r\n```\r\nfrom __future__ import print_function\r\n\r\nimport psycopg2\r\nimport select\r\nimport socket\r\nimport traceback\r\n\r\n\r\ndef wait(conn):\r\n    while 1:\r\n        state = conn.poll()\r\n        if state == psycopg2.extensions.POLL_OK:\r\n            break\r\n        elif state == psycopg2.extensions.POLL_WRITE:\r\n            select.select([], [conn.fileno()], [])\r\n        elif state == psycopg2.extensions.POLL_READ:\r\n            select.select([conn.fileno()], [], [])\r\n        else:\r\n            raise psycopg2.OperationalError(\"poll() returned %s\" % state)\r\n\r\n\r\naconn = psycopg2.connect(dsn=\"user=foo password=bar host=127.0.0.1 port=5432\", async=1)\r\nwait(aconn)\r\n\r\ns = socket.fromfd(aconn.fileno(), socket.AF_INET, socket.SOCK_STREAM)\r\ns.shutdown(socket.SHUT_RD)\r\n\r\ntry:\r\n    acur = aconn.cursor()\r\n    acur.execute(\"SELECT 1\")\r\n    wait(aconn)\r\nexcept Exception as err:\r\n    print(\"Execution failed: %s\" % err)\r\n    traceback.print_exc()\r\n    print(\"aconn.closed = %s\" % aconn.closed)\r\nelse:\r\n    print(acur.fetchone())\r\n```\r\n\r\nAnd the result is:\r\n\r\n```\r\n$ python asynclose.py \r\nExecution failed: SSL SYSCALL error: EOF detected\r\n\r\nTraceback (most recent call last):\r\n  File \"asynclose.py\", line 35, in <module>\r\n    wait(aconn)\r\n  File \"asynclose.py\", line 12, in wait\r\n    state = conn.poll()\r\nOperationalError: SSL SYSCALL error: EOF detected\r\n\r\naconn.closed = 2\r\n```"}}