You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by va...@apache.org on 2026/06/17 13:44:34 UTC
(superset) branch master updated: fix(celery): check app context before session removal in teardown (#37574)
This is an automated email from the ASF dual-hosted git repository.
Vitor-Avila pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 750518cf6fb fix(celery): check app context before session removal in teardown (#37574)
750518cf6fb is described below
commit 750518cf6fb660538afce6bcbe00506b61d1ceab
Author: Joao Amaral <72...@users.noreply.github.com>
AuthorDate: Wed Jun 17 10:44:27 2026 -0300
fix(celery): check app context before session removal in teardown (#37574)
Co-authored-by: codeant-ai-for-open-source[bot] <24...@users.noreply.github.com>
Co-authored-by: Đỗ Trọng Hải <41...@users.noreply.github.com>
Co-authored-by: Daniel Vaz Gaspar <da...@gmail.com>
Co-authored-by: Claude Opus 4.7 (1M context) <no...@anthropic.com>
Co-authored-by: Elizabeth Thompson <es...@gmail.com>
---
superset/tasks/celery_app.py | 9 ++++-----
tests/integration_tests/celery_tests.py | 35 +++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/superset/tasks/celery_app.py b/superset/tasks/celery_app.py
index 2049246f042..96d64896895 100644
--- a/superset/tasks/celery_app.py
+++ b/superset/tasks/celery_app.py
@@ -23,6 +23,7 @@ it needs to call create_app() in order to initialize things properly
from typing import Any
from celery.signals import task_postrun, worker_process_init
+from flask import has_app_context
# Superset framework imports
from superset import create_app
@@ -63,9 +64,7 @@ def teardown( # pylint: disable=unused-argument
:see: https://gist.github.com/twolfson/a1b329e9353f9b575131
"""
- if flask_app.config.get("SQLALCHEMY_COMMIT_ON_TEARDOWN"):
- if not isinstance(retval, Exception):
- db.session.commit() # pylint: disable=consider-using-transaction
-
if not flask_app.config.get("CELERY_ALWAYS_EAGER"):
- db.session.remove()
+ # Ensure session is removed only inside flask app context
+ if has_app_context():
+ db.session.remove()
diff --git a/tests/integration_tests/celery_tests.py b/tests/integration_tests/celery_tests.py
index 11f9cdf0707..7d17019b1d3 100644
--- a/tests/integration_tests/celery_tests.py
+++ b/tests/integration_tests/celery_tests.py
@@ -495,6 +495,41 @@ def test_in_app_context():
)
+def test_teardown_without_app_context():
+ """Test teardown skips db.session.remove() outside app context.
+
+ Regression test for https://github.com/apache/superset/issues/36892
+ The task_postrun signal can fire after the app context is torn down,
+ so teardown() must check has_app_context() before calling db.session.remove().
+ """
+ # Guard: if superset.tasks.celery_app hasn't been imported yet, its module-level
+ # `flask_app = create_app()` would spin up a second Flask app and corrupt the
+ # Flask-AppBuilder view-registry singleton used by the rest of the test suite.
+ # Patching create_app to return the already-created test app prevents that.
+ with mock.patch("superset.create_app", return_value=app):
+ from superset.tasks.celery_app import teardown
+
+ with (
+ mock.patch("superset.tasks.celery_app.has_app_context", return_value=False),
+ mock.patch("superset.tasks.celery_app.db.session.remove") as mock_remove,
+ ):
+ teardown(retval="success")
+ mock_remove.assert_not_called()
+
+
+def test_teardown_with_app_context():
+ """Test teardown calls db.session.remove() inside app context."""
+ with mock.patch("superset.create_app", return_value=app):
+ from superset.tasks.celery_app import teardown
+
+ with (
+ mock.patch("superset.tasks.celery_app.has_app_context", return_value=True),
+ mock.patch("superset.tasks.celery_app.db.session.remove") as mock_remove,
+ ):
+ teardown(retval="success")
+ mock_remove.assert_called_once()
+
+
def delete_tmp_view_or_table(name: str, ctas_method: CTASMethod):
db.get_engine().execute(f"DROP {ctas_method.name} IF EXISTS {name}")