Server closed the connection unexpectedly

I’m running a Flask backend service, Postgres database and a React frontend service. Locally, these three work together just fine. When I try deploying to Render, I get the following backend logs, which suggest something with the database is going wrong. However, I don’t know what. Can someone help me understand what’s going wrong and why?

Jun 22 12:59:52 PM  Traceback (most recent call last):
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1820, in _execute_context
Jun 22 12:59:52 PM      cursor, statement, parameters, context
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
Jun 22 12:59:52 PM      cursor.execute(statement, parameters)
Jun 22 12:59:52 PM  psycopg2.OperationalError: server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.
Jun 22 12:59:52 PM  server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.
Jun 22 12:59:52 PM  server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.
Jun 22 12:59:52 PM  
Jun 22 12:59:52 PM  
Jun 22 12:59:52 PM  The above exception was the direct cause of the following exception:
Jun 22 12:59:52 PM  
Jun 22 12:59:52 PM  Traceback (most recent call last):
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 2077, in wsgi_app
Jun 22 12:59:52 PM      response = self.full_dispatch_request()
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1525, in full_dispatch_request
Jun 22 12:59:52 PM      rv = self.handle_user_exception(e)
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
Jun 22 12:59:52 PM      return cors_after_request(app.make_response(f(*args, **kwargs)))
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1523, in full_dispatch_request
Jun 22 12:59:52 PM      rv = self.dispatch_request()
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/flask/app.py", line 1509, in dispatch_request
Jun 22 12:59:52 PM      return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
Jun 22 12:59:52 PM    File "/opt/render/project/src/app.py", line 344, in signup
Jun 22 12:59:52 PM      user = User.lookup(username=username)
Jun 22 12:59:52 PM    File "/opt/render/project/src/src/db/models.py", line 98, in lookup
Jun 22 12:59:52 PM      return cls.query.filter_by(username=username).one_or_none()
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 2845, in one_or_none
Jun 22 12:59:52 PM      return self._iter().one_or_none()
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 2906, in _iter
Jun 22 12:59:52 PM      execution_options={"_sa_orm_load_options": self.load_options},
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1712, in execute
Jun 22 12:59:52 PM      result = conn._execute_20(statement, params or {}, execution_options)
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1631, in _execute_20
Jun 22 12:59:52 PM      return meth(self, args_10style, kwargs_10style, execution_options)
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 326, in _execute_on_connection
Jun 22 12:59:52 PM      self, multiparams, params, execution_options
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1508, in _execute_clauseelement
Jun 22 12:59:52 PM      cache_hit=cache_hit,
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1863, in _execute_context
Jun 22 12:59:52 PM      e, statement, parameters, cursor, context
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2044, in _handle_dbapi_exception
Jun 22 12:59:52 PM      sqlalchemy_exception, with_traceback=exc_info[2], from_=e
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
Jun 22 12:59:52 PM      raise exception
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1820, in _execute_context
Jun 22 12:59:52 PM      cursor, statement, parameters, context
Jun 22 12:59:52 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
Jun 22 12:59:52 PM      cursor.execute(statement, parameters)
Jun 22 12:59:52 PM  sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.
Jun 22 12:59:52 PM  server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.
Jun 22 12:59:52 PM  server closed the connection unexpectedly
Jun 22 12:59:52 PM      This probably means the server terminated abnormally
Jun 22 12:59:52 PM      before or while processing the request.

I can find related problems , but I don’t know whether they’re relevant to Render. How can I find out whether this error is caused by Render or something else?

Hi Rylan,

Thanks for reaching out.

When we’ve seen similar SQLAlchemy issues in the past, adding an env var of SQLALCHEMY_ENABLE_POOL_PRE_PING with a value of true seems to resolve it. Could you give that a try and see if it helps?

Thanks

Alan

Hey Rylan,

Apologies! That env var is used by a specific package (Redash).

I think in your case you’d need to add the pool_pre_ping=True option to the SQLAlchemy connection setup in your code.

Kind regards

Alan

1 Like

Yep! That did it. I’m not sure why this error isn’t more descriptive, but thank you for confirming that the fix I found is correct :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.