Skip to content
New issue

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

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

Already on GitHub? # to your account

Postgres doesn't reconnect after idle time #541

Closed
Korayem opened this issue Jul 7, 2024 · 3 comments
Closed

Postgres doesn't reconnect after idle time #541

Korayem opened this issue Jul 7, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Korayem
Copy link

Korayem commented Jul 7, 2024

Describe the bug
connect_to_postgres() code aparently doesn't handle situations where connection is closed after some idle time

To Reproduce
Steps to reproduce the behavior:

  1. Run Vanna using postgres
  2. Try to chat in any interface, things work
  3. Leave it running for a few minutes
  4. Try to chat again
  5. Error: psycopg2.InterfaceError: connection already closed

Expected behavior
App should detect connection is closed and re-establish a new connection

Error logs/Screenshots
psycopg2.InterfaceError: connection already closed

Desktop (please complete the following information where):

  • OS: Mac OS
  • Version: Sonoma 14.3.1
  • Python: 3.11
  • Vanna: 0.6.2

Additional context
Potential Solution: https://stackoverflow.com/a/74328338/80434

try:
    cursor = conn.cursor()
except psycopg2.InterfaceError as e:
    print('{} - connection will be reset'.format(e))
    # Close old connection 
    if conn:
        if cursor:
            cursor.close()
        conn.close()
    conn = None
    cursor = None
    
    # Reconnect 
    conn = psycopg2.connect(user=user,
                            password=password,
                            host=host,
                            port=port,
                            database=database,
                            sslmode=sslmode)
    cursor = conn.cursor()
@alexfire68
Copy link

+1

@pygeek
Copy link
Contributor

pygeek commented Jul 25, 2024

This error is also raised when one attempts to close an already closed connection. Re-establishing a connection, in this case, would be undesired behavior. This is likely also a feature we would want to apply across the board. I recommend an implementation similar to: https://github.com/django/django/blob/main/django/db/backends/base/base.py

Also, what are your configuration settings/plugins? Postgres should be keeping connections alive indefinitely by default

sinjup pushed a commit to sinjup/vanna that referenced this issue Aug 20, 2024
zainhoda added a commit that referenced this issue Aug 21, 2024
fix: Postgres doesn't reconnect after idle time #541
@Korayem
Copy link
Author

Korayem commented Aug 24, 2024

Fixed here #607

@Korayem Korayem closed this as completed Aug 24, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants