Skip to content

MWAA: psycopg2.OperationalError: SSL connection has been closed unexpectedly

2

Since we update MWAA version to 2.7.2+ we start facing issue when using Dynamic Task Mapping and EMR operator(EmrServerlessStartJobOperator, EmrContainerOperator) or some airflow tasks(probably with botocore waiters):

[2024-10-08, 16:38:05 UTC] {{taskinstance.py:2698}} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1094, in _commit_impl
    self.engine.dialect.do_commit(self.connection)
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 686, in do_commit
    dbapi_connection.commit()
psycopg2.OperationalError: SSL connection has been closed unexpectedly


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/models/taskinstance.py", line 434, in _execute_task
    with create_session() as session:
  File "/usr/local/lib/python3.11/contextlib.py", line 144, in __exit__
    next(self.gen)
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/airflow/utils/session.py", line 39, in create_session
    session.commit()
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 1454, in commit
    self._transaction.commit(_to_root=self.future)
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line 839, in commit
    trans.commit()
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2469, in commit
    self._do_commit()
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2659, in _do_commit
    self._connection_commit_impl()
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2630, in _connection_commit_impl
    self.connection._commit_impl()
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1096, in _commit_impl
    self._handle_dbapi_exception(e, None, None, None, None)
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2134, in _handle_dbapi_exception
    util.raise_(
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1094, in _commit_impl
    self.engine.dialect.do_commit(self.connection)
  File "/usr/local/airflow/.local/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 686, in do_commit
    dbapi_connection.commit()
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) SSL connection has been closed unexpectedly

(Background on this error at: https://sqlalche.me/e/14/e3q8)

Airflow task itself was successful, but it failed on exit and only after ~30 minutes of runtime, if task runs lower than this time, there are no issues. Any help is appreciated, we are investigating some custom settings like:

  • sql_alchemy_connect_args
# In MWAA cluster settings
AIRFLOW__CORE__SQL_ALCHEMY_CONNECT_ARGS='{"keepalives":1, "keepalives_idle":60, "keepalives_interval":10, "keepalives_count":10}'
  • celery.worker_autoscale(might be OOM memory issue because of while loop?)
  • AIRFLOW__CORE__SQL_ALCHEMY_POOL_RECYCLE
core.sql_alchemy_pool_recycle=86400
4 Answers
0

The issue you're experiencing with SSL connections being closed unexpectedly after upgrading to MWAA version 2.7.2+ is likely related to connection timeouts or network stability issues. This problem can occur when long-running tasks exceed the default connection timeout settings.

To address this, you're on the right track by investigating custom settings. The SQL Alchemy connect args you've mentioned are a good start:

AIRFLOW__CORE__SQL_ALCHEMY_CONNECT_ARGS='{"keepalives":1, "keepalives_idle":60, "keepalives_interval":10, "keepalives_count":10}'

These settings help maintain the connection by sending keepalive packets. However, you might need to adjust these values further based on your specific environment and task durations.

In addition to the SQL Alchemy connect args, consider the following approaches:

  1. Increase the connection pool recycle time: AIRFLOW__CORE__SQL_ALCHEMY_POOL_RECYCLE=1800

    This setting (in seconds) determines how long a connection can be idle before being recycled.

  2. Adjust the Celery broker connection timeout: AIRFLOW__CELERY__BROKER_CONNECTION_TIMEOUT=60

    This can help with timeouts related to the Celery broker.

  3. If memory issues are suspected, you can try adjusting the worker concurrency: AIRFLOW__CELERY__WORKER_CONCURRENCY=16

    Reduce this value if you're experiencing out-of-memory errors.

  4. For the Celery worker autoscale settings, ensure they are configured appropriately for your workload: AIRFLOW__CELERY__WORKER_AUTOSCALE='16,8'

    This sets the maximum and minimum number of worker processes.

  5. Consider implementing retry logic in your DAGs for tasks that are prone to these connection issues.

  6. If the problem persists, you may need to investigate network-level issues, such as VPC configurations or security group settings that might be affecting the stability of your connections.

Remember to test these changes incrementally and monitor their impact on your workflow performance. If the issue continues, you may need to reach out to AWS support for further assistance, as there could be underlying infrastructure or service-specific factors at play.
Sources
Troubleshooting: DAGs, Operators, Connections, and other issues in Apache Airflow v1 - Amazon Managed Workflows for Apache Airflow
Apache Airflow versions on Amazon Managed Workflows for Apache Airflow - Amazon Managed Workflows for Apache Airflow
Amazon Managed Workflows for Apache Airflow (MWAA) Features – Amazon Web Services

answered 2 years ago

0

AWS team, any ideas?

answered 2 years ago

0

AWS team, any ideas?

answered 2 years ago

0

AWS team, any ideas?

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.