getredash / redash

Make Your Company Data Driven. Connect to any data source, easily visualize, dashboard and share your data.
http://redash.io/
BSD 2-Clause "Simplified" License
26.47k stars 4.38k forks source link

Redsah returns HTTP 500 after PostgreSQL connection was closed #3817

Open citrin opened 5 years ago

citrin commented 5 years ago

Issue Summary

Redsah returns HTTP 500 after PostgreSQL connection was closed, e. g. if PostgreSQL was restarted.

Steps to Reproduce

  1. Restart PostgreSQL
  2. Send HTTP request to redash.

Technical details:

It is easy to fix this issue by using pre-ping in SQLAlchemy

diff --git redash/settings/__init__.py redash/settings/__init__.py
index efb2a1cd..8e00fc9f 100644
--- redash/settings/__init__.py
+++ redash/settings/__init__.py
@@ -29,6 +29,10 @@ SQLALCHEMY_DATABASE_URI = os.environ.get("REDASH_DATABASE_URL", os.environ.get('
 SQLALCHEMY_MAX_OVERFLOW = int_or_none(os.environ.get("SQLALCHEMY_MAX_OVERFLOW"))
 SQLALCHEMY_POOL_SIZE = int_or_none(os.environ.get("SQLALCHEMY_POOL_SIZE"))
 SQLALCHEMY_DISABLE_POOL = parse_boolean(os.environ.get("SQLALCHEMY_DISABLE_POOL", "false"))
+SQLALCHEMY_ENGINE_OPTIONS = {
+    # https://docs.sqlalchemy.org/en/13/core/pooling.html#pool-disconnects
+    'pool_pre_ping': True,
+}
 SQLALCHEMY_TRACK_MODIFICATIONS = False
 SQLALCHEMY_ECHO = False

diff --git requirements.txt requirements.txt
index b3b0d668..c56bd668 100644
--- requirements.txt
+++ requirements.txt
@@ -12,7 +12,7 @@ Flask-Login==0.4.0
 Flask-OAuthLib==0.9.5
 # pin this until https://github.com/lepture/flask-oauthlib/pull/388 is released
 requests-oauthlib>=0.6.2,<1.2.0
-Flask-SQLAlchemy==2.3.2
+Flask-SQLAlchemy==2.4.0
 Flask-Migrate==2.0.1
 flask-mail==0.9.1
 flask-sslify==0.1.5

Flask-SQLAlchemy 2.4.0 deprecated a few settings - they should be moved to SQLALCHEMY_ENGINE_OPTIONS too.

arikfr commented 5 years ago

If you were to run a new request after this, will it error again?

citrin commented 5 years ago

No, this errors happens only on a first request (or a few first requests) after database connection was closed. Then redash reconnects to PostgreSQL.