You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by vi...@apache.org on 2026/08/24 17:15:53 UTC

(superset) branch villebro/gtf-async-robustness created (now a4a4ce7b3ea)

This is an automated email from the ASF dual-hosted git repository.

villebro pushed a change to branch villebro/gtf-async-robustness
in repository https://gitbox.apache.org/repos/asf/superset.git


      at a4a4ce7b3ea fix(gtf): async chart-data robustness + accurate lock errors; retire async_events

This branch includes the following new commits:

     new a4a4ce7b3ea fix(gtf): async chart-data robustness + accurate lock errors; retire async_events

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(superset) 01/01: fix(gtf): async chart-data robustness + accurate lock errors; retire async_events

Posted by vi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

villebro pushed a commit to branch villebro/gtf-async-robustness
in repository https://gitbox.apache.org/repos/asf/superset.git

commit a4a4ce7b3eafa8b81fe76a43e8c535b1ff4d752b
Author: Ville Brofeldt <vi...@gmail.com>
AuthorDate: Mon Aug 24 09:46:59 2026 -0700

    fix(gtf): async chart-data robustness + accurate lock errors; retire async_events
    
    Bundles the post-cutover review follow-ups:
    
    - Surface the true DB error on task create/update. on_error gains an opt-in
      preserve_message that carries the DBAPI cause (e.g. "database is locked")
      into TaskCreateFailedError / TaskUpdateFailedError instead of the generic
      message, so a transient contention failure is no longer reported as a flat
      "Task could not be created."
    
    - Don't run async when the result can't be read back. /chart/data refuses async
      under a NullCache DATA backend (falls back to sync 200), and the client falls
      back to a synchronous fetch if a post-completion re-request still returns 202
      (oversized result / per-query disabled timeout), instead of looping/throwing.
    
    - Ownership-checked lock release is now atomic. A Lua compare-and-delete on the
      coordination backend replaces the get-then-delete in _release_redis, closing
      the window where an expired-then-reacquired lock could be dropped.
    
    - A dependent cancelled while waiting on prerequisites no longer publishes a
      FAILURE that never committed: execute_task only publishes FAILURE when the
      conditional transition actually landed, else reports the committed status.
    
    - Retire the vestigial async_events package: its only survivor, cache_backend
      (the coordination service's Redis client wrappers), moves to
      superset/coordination/cache_backend.py and the package is deleted.
---
 .../src/components/Chart/chartAction.ts            | 25 +++++++---
 .../src/components/Chart/chartActions.test.ts      | 38 +++++++++++++++
 superset/async_events/__init__.py                  | 16 ------
 superset/charts/data/api.py                        |  8 ++-
 superset/commands/distributed_lock/release.py      | 25 ++++------
 superset/commands/tasks/internal_update.py         |  8 ++-
 superset/commands/tasks/submit.py                  |  8 ++-
 superset/coordination/base.py                      | 20 ++++++++
 .../cache_backend.py                               | 33 +++++++++++++
 superset/coordination/types.py                     |  2 +-
 superset/tasks/scheduler.py                        | 16 ++++--
 superset/utils/cache_manager.py                    |  4 +-
 superset/utils/decorators.py                       | 10 ++++
 tests/unit_tests/async_events/__init__.py          | 16 ------
 tests/unit_tests/charts/test_chart_data_api.py     | 14 +++++-
 .../test_cache_backend.py                          | 43 ++++++++++------
 .../distributed_lock/distributed_lock_tests.py     | 39 ++++++++++-----
 tests/unit_tests/tasks/test_dependencies.py        | 57 ++++++++++++++++++++++
 tests/unit_tests/utils/test_decorators.py          | 29 +++++++++++
 19 files changed, 321 insertions(+), 90 deletions(-)

diff --git a/superset-frontend/src/components/Chart/chartAction.ts b/superset-frontend/src/components/Chart/chartAction.ts
index 7f76b28a62d..ef18a3abb3c 100644
--- a/superset-frontend/src/components/Chart/chartAction.ts
+++ b/superset-frontend/src/components/Chart/chartAction.ts
@@ -745,8 +745,10 @@ export function exploreJSON(
 
     // Re-issue the chart-data request. On the async path this runs after every
     // query task has succeeded, so `force` is dropped — the per-query DATA cache
-    // is warm and this returns synchronously (200) from cache.
-    const requestChartData = (fromCache = false) =>
+    // is warm and this returns synchronously (200) from cache. `forceSync` opts
+    // out of async entirely (used as the fallback below when the result could
+    // not be cached), so the server returns the payload inline rather than a 202.
+    const requestChartData = (fromCache = false, forceSync = false) =>
       getChartDataRequest({
         setDataMask,
         formData,
@@ -756,7 +758,7 @@ export function exploreJSON(
         requestParams,
         ownState,
         // exploreJSON handles 202 via handleChartDataResponse.
-        enableAsyncMode: true,
+        enableAsyncMode: !forceSync,
       });
 
     const chartDataRequest = requestChartData();
@@ -767,9 +769,20 @@ export function exploreJSON(
           response,
           json,
           () =>
-            requestChartData(true).then(({ response: r, json: j }) =>
-              handleChartDataResponse(r, j),
-            ) as Promise<QueryData[]>,
+            requestChartData(true).then(({ response: r, json: j }) => {
+              // Tasks succeeded, but if the result was never cached the
+              // re-request returns another 202 (NullCache is refused server-side,
+              // yet an oversized result or a per-query disabled timeout still
+              // skips the write). Fall back to a synchronous fetch, which returns
+              // the payload inline, instead of looping on an uncacheable request.
+              if (r.status === 202) {
+                return requestChartData(true, true).then(
+                  ({ response: sr, json: sj }) =>
+                    handleChartDataResponse(sr, sj),
+                ) as Promise<QueryData[]>;
+              }
+              return handleChartDataResponse(r, j);
+            }) as Promise<QueryData[]>,
           controller.signal,
         ),
       )
diff --git a/superset-frontend/src/components/Chart/chartActions.test.ts b/superset-frontend/src/components/Chart/chartActions.test.ts
index 42197977671..eb495ad16ec 100644
--- a/superset-frontend/src/components/Chart/chartActions.test.ts
+++ b/superset-frontend/src/components/Chart/chartActions.test.ts
@@ -578,6 +578,44 @@ describe('chart actions', () => {
           'validation failed',
         );
       });
+
+      test('falls back to a synchronous fetch when the async re-request is still uncacheable', async () => {
+        // 1: initial async 202; 2: the post-completion refetch is STILL a 202
+        // (the result could not be cached — oversized value / disabled per-query
+        // timeout); 3: the forced-sync fetch returns the payload inline (200).
+        fetchMock.removeRoute(MOCK_URL);
+        let calls = 0;
+        fetchMock.post(
+          `glob:*${MOCK_URL}*`,
+          () => {
+            calls += 1;
+            return calls >= 3
+              ? { status: 200, body: { result: [{ data: [1, 2, 3] }] } }
+              : { status: 202, body: { result: [{ job_id: 'job-1' }] } };
+          },
+          { name: MOCK_URL },
+        );
+
+        const actionThunk = actions.postChartFormData(
+          { viz_type: 'my_viz' } as QueryFormData,
+          false,
+          undefined,
+          undefined,
+        );
+        await actionThunk(
+          dispatch as unknown as actions.ChartThunkDispatch,
+          mockGetState as unknown as () => actions.RootState,
+          undefined,
+        );
+
+        // Three requests total (async, still-202 refetch, sync fallback), and the
+        // chart resolves successfully rather than throwing on the repeat 202.
+        expect(calls).toBe(3);
+        const succeeded = dispatch.mock.calls.find(
+          ([action]) => action?.type === actions.CHART_UPDATE_SUCCEEDED,
+        )?.[0];
+        expect(succeeded).toBeDefined();
+      });
     });
   });
 
diff --git a/superset/async_events/__init__.py b/superset/async_events/__init__.py
deleted file mode 100644
index 13a83393a91..00000000000
--- a/superset/async_events/__init__.py
+++ /dev/null
@@ -1,16 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
diff --git a/superset/charts/data/api.py b/superset/charts/data/api.py
index 0fa02a3b5a5..f80bec58984 100644
--- a/superset/charts/data/api.py
+++ b/superset/charts/data/api.py
@@ -25,6 +25,7 @@ from typing import Any, Callable, TYPE_CHECKING
 from flask import current_app as app, make_response, request, Response
 from flask_appbuilder.api import expose, protect
 from flask_babel import gettext as _
+from flask_caching.backends import NullCache
 from marshmallow import ValidationError
 from werkzeug.utils import secure_filename
 
@@ -52,7 +53,7 @@ from superset.connectors.sqla.models import BaseDatasource
 from superset.constants import CACHE_DISABLED_TIMEOUT
 from superset.daos.exceptions import DatasourceNotFound
 from superset.exceptions import QueryObjectValidationError, SupersetSecurityException
-from superset.extensions import event_logger
+from superset.extensions import cache_manager, event_logger
 from superset.models.sql_lab import Query
 from superset.tasks.async_queries import submit_chart_data_query_tasks
 from superset.utils import json
@@ -362,6 +363,10 @@ class ChartDataRestApi(ChartRestApi):
         200 flow). It is only available when ``GLOBAL_ASYNC_QUERIES`` is enabled, the
         result is a full JSON payload, and caching is on (async delivery reads the
         result back from the DATA cache).
+
+        A ``NullCache`` DATA backend can never satisfy the read-back, so async is
+        refused for it and the request runs synchronously — otherwise every chart
+        would schedule tasks, succeed, and then loop on an uncacheable re-request.
         """
         return (
             bool(json_body.get("async_mode"))
@@ -369,6 +374,7 @@ class ChartDataRestApi(ChartRestApi):
             and query_context.result_format == ChartDataResultFormat.JSON
             and query_context.result_type == ChartDataResultType.FULL
             and cache_timeout != CACHE_DISABLED_TIMEOUT
+            and not isinstance(cache_manager.data_cache.cache, NullCache)
         )
 
     def _run_async(
diff --git a/superset/commands/distributed_lock/release.py b/superset/commands/distributed_lock/release.py
index b8a222c991d..5537da007af 100644
--- a/superset/commands/distributed_lock/release.py
+++ b/superset/commands/distributed_lock/release.py
@@ -64,26 +64,21 @@ class ReleaseDistributedLock(BaseDistributedLockCommand):
             self._release_kv()
 
     def _release_redis(self) -> None:
-        """Release the lock only if we still own it (compare-and-delete)."""
+        """Release the lock only if we still own it (atomic compare-and-delete)."""
         try:
             if self.token is not None:
-                stored = CoordinationService.get_value(self.redis_lock_key)
-                if stored is None:
-                    # Already gone (expired or released) — nothing to do.
-                    return
-                stored_token = stored.decode() if isinstance(stored, bytes) else stored
-                if stored_token != self.token:
-                    # A different acquisition owns the key now (ours expired);
-                    # deleting it would drop the current holder's lock.
+                # One server-side compare-and-delete: removes the key only if it
+                # still holds our token, so an expired-then-reacquired lock owned
+                # by another holder is left untouched (no get-then-delete window).
+                if not CoordinationService.compare_and_delete(
+                    self.redis_lock_key, self.token
+                ):
                     logger.warning(
-                        "Not releasing Redis lock %s: owned by another acquisition",
+                        "Not releasing Redis lock %s: owned by another "
+                        "acquisition or already gone",
                         self.redis_lock_key,
                     )
-                    return
-            # NOTE: the get-then-delete above is not atomic; the residual window
-            # (ours expires and is re-acquired between the get and the delete) is
-            # bounded by the round-trip and vastly smaller than an unconditional
-            # delete's exposure. A Lua compare-and-delete would close it fully.
+                return
             CoordinationService.delete_value(self.redis_lock_key)
             logger.debug("Released Redis lock: %s", self.redis_lock_key)
         except redis.RedisError as ex:
diff --git a/superset/commands/tasks/internal_update.py b/superset/commands/tasks/internal_update.py
index c6cc353c1e5..1db75f3c1ab 100644
--- a/superset/commands/tasks/internal_update.py
+++ b/superset/commands/tasks/internal_update.py
@@ -102,7 +102,9 @@ class InternalUpdateTaskCommand(BaseCommand):
             _publish_entity_change(self._task_uuid)
         return updated
 
-    @transaction(on_error=partial(on_error, reraise=TaskUpdateFailedError))
+    @transaction(
+        on_error=partial(on_error, reraise=TaskUpdateFailedError, preserve_message=True)
+    )
     def _run_in_transaction(self) -> bool:
         """
         Execute zero-read update.
@@ -194,7 +196,9 @@ class InternalStatusTransitionCommand(BaseCommand):
             _publish_entity_change(self._task_uuid)
         return updated
 
-    @transaction(on_error=partial(on_error, reraise=TaskUpdateFailedError))
+    @transaction(
+        on_error=partial(on_error, reraise=TaskUpdateFailedError, preserve_message=True)
+    )
     def _run_in_transaction(self) -> bool:
         """
         Execute atomic conditional status update.
diff --git a/superset/commands/tasks/submit.py b/superset/commands/tasks/submit.py
index c2b89881d1c..578ad678829 100644
--- a/superset/commands/tasks/submit.py
+++ b/superset/commands/tasks/submit.py
@@ -60,7 +60,9 @@ class SubmitTaskCommand(BaseCommand):
     def __init__(self, data: dict[str, Any]):
         self._properties = data.copy()
 
-    @transaction(on_error=partial(on_error, reraise=TaskCreateFailedError))
+    @transaction(
+        on_error=partial(on_error, reraise=TaskCreateFailedError, preserve_message=True)
+    )
     def run(self) -> "Task":
         """
         Execute the command with distributed locking.
@@ -73,7 +75,9 @@ class SubmitTaskCommand(BaseCommand):
         task, _ = self.run_with_info()
         return task
 
-    @transaction(on_error=partial(on_error, reraise=TaskCreateFailedError))
+    @transaction(
+        on_error=partial(on_error, reraise=TaskCreateFailedError, preserve_message=True)
+    )
     def run_with_info(self) -> tuple["Task", bool]:
         """
         Execute the command and return (task, is_new) tuple.
diff --git a/superset/coordination/base.py b/superset/coordination/base.py
index e05f63ea091..b2d17656eb5 100644
--- a/superset/coordination/base.py
+++ b/superset/coordination/base.py
@@ -237,6 +237,26 @@ class CoordinationService:
             *(cls._resolve_key(key) for key in keys)
         )
 
+    @classmethod
+    def compare_and_delete(
+        cls, key: KeyLike, expected: str, backend: "CoordinationBackend | None" = None
+    ) -> int:
+        """Atomically delete ``key`` only if its value still equals ``expected``.
+
+        Single server-side operation, so it closes the TTL-expiry race a separate
+        get-then-delete leaves open (used for ownership-checked lock release).
+
+        :param key: a literal key string, or a ``() -> str`` generator (see
+            :meth:`_resolve_key`).
+        :param expected: the value the key must currently hold to be deleted.
+        :param backend: optional explicit backend (see :meth:`_require_backend`).
+        :returns: 1 if the key was deleted, 0 otherwise.
+        :raises CoordinationBackendUnavailableError: if no backend is available.
+        """
+        return cls._require_backend(backend).compare_and_delete(
+            cls._resolve_key(key), expected
+        )
+
     # -- Streams -------------------------------------------------------------
 
     @classmethod
diff --git a/superset/async_events/cache_backend.py b/superset/coordination/cache_backend.py
similarity index 92%
rename from superset/async_events/cache_backend.py
rename to superset/coordination/cache_backend.py
index e0ae28cfaeb..32a4e766ce5 100644
--- a/superset/async_events/cache_backend.py
+++ b/superset/coordination/cache_backend.py
@@ -22,6 +22,19 @@ import redis
 from flask_caching.backends.rediscache import RedisCache, RedisSentinelCache
 from redis.sentinel import Sentinel
 
+# Atomic compare-and-delete: delete the key only if its current value still
+# equals the caller's token. Runs server-side in one round trip, so it closes
+# the TTL-expiry race a separate GET-then-DEL leaves open (the key expiring and
+# being re-acquired by another holder between the two calls). Returns 1 if the
+# key was deleted, 0 otherwise.
+_COMPARE_AND_DELETE_LUA = """
+if redis.call('get', KEYS[1]) == ARGV[1] then
+    return redis.call('del', KEYS[1])
+else
+    return 0
+end
+"""
+
 
 class RedisCacheBackend(RedisCache):
     MAX_EVENT_COUNT = 100
@@ -117,6 +130,16 @@ class RedisCacheBackend(RedisCache):
         """
         return self._cache.delete(*names)
 
+    def compare_and_delete(self, name: str, expected: str) -> int:
+        """
+        Atomically delete ``name`` only if its value still equals ``expected``.
+
+        :param name: Key name
+        :param expected: The value the key must currently hold to be deleted
+        :returns: 1 if the key was deleted, 0 otherwise
+        """
+        return int(self._cache.eval(_COMPARE_AND_DELETE_LUA, 1, name, expected))
+
     def publish(self, channel: str, message: str) -> int:
         """
         Publish a message to a Redis pub/sub channel.
@@ -351,6 +374,16 @@ class RedisSentinelCacheBackend(RedisSentinelCache):
         """
         return self._cache.delete(*names)
 
+    def compare_and_delete(self, name: str, expected: str) -> int:
+        """
+        Atomically delete ``name`` only if its value still equals ``expected``.
+
+        :param name: Key name
+        :param expected: The value the key must currently hold to be deleted
+        :returns: 1 if the key was deleted, 0 otherwise
+        """
+        return int(self._cache.eval(_COMPARE_AND_DELETE_LUA, 1, name, expected))
+
     def publish(self, channel: str, message: str) -> int:
         """
         Publish a message to a Redis pub/sub channel.
diff --git a/superset/coordination/types.py b/superset/coordination/types.py
index a2145610463..c43e2375e60 100644
--- a/superset/coordination/types.py
+++ b/superset/coordination/types.py
@@ -23,7 +23,7 @@ import threading
 from typing import TYPE_CHECKING
 
 if TYPE_CHECKING:
-    from superset.async_events.cache_backend import (
+    from superset.coordination.cache_backend import (
         RedisCacheBackend,
         RedisSentinelCacheBackend,
     )
diff --git a/superset/tasks/scheduler.py b/superset/tasks/scheduler.py
index 6a0db9b2fdc..d381813c670 100644
--- a/superset/tasks/scheduler.py
+++ b/superset/tasks/scheduler.py
@@ -395,7 +395,7 @@ def execute_task(  # noqa: C901
             failed_prerequisite.uuid,
             failed_prerequisite.status,
         )
-        InternalStatusTransitionCommand(
+        failed_transition = InternalStatusTransitionCommand(
             task_uuid=native_uuid,
             new_status=TaskStatus.FAILURE,
             expected_status=[TaskStatus.PENDING, TaskStatus.ABORTING],
@@ -407,8 +407,18 @@ def execute_task(  # noqa: C901
                 )
             },
         ).run()
-        TaskManager.publish_completion(native_uuid, TaskStatus.FAILURE.value)
-        return {"status": TaskStatus.FAILURE.value, "task_uuid": task_uuid}
+        if failed_transition:
+            TaskManager.publish_completion(native_uuid, TaskStatus.FAILURE.value)
+            return {"status": TaskStatus.FAILURE.value, "task_uuid": task_uuid}
+        # The dependent was moved to a terminal state concurrently (e.g. aborted
+        # while waiting on prerequisites), so the FAILURE transition was a no-op.
+        # Report the status that actually committed rather than publishing a
+        # FAILURE completion that contradicts the DB.
+        refreshed = TaskDAO.find_one_or_none(uuid=native_uuid, skip_base_filter=True)
+        return {
+            "status": refreshed.status if refreshed else "unknown",
+            "task_uuid": task_uuid,
+        }
 
     # Atomic transition: PENDING → IN_PROGRESS (set started_at for duration tracking)
     if not InternalStatusTransitionCommand(
diff --git a/superset/utils/cache_manager.py b/superset/utils/cache_manager.py
index 509c9b10110..00dda0fd38e 100644
--- a/superset/utils/cache_manager.py
+++ b/superset/utils/cache_manager.py
@@ -27,7 +27,7 @@ from markupsafe import Markup
 from superset.utils.core import DatasourceType
 
 if TYPE_CHECKING:
-    from superset.async_events.cache_backend import (
+    from superset.coordination.cache_backend import (
         RedisCacheBackend,
         RedisSentinelCacheBackend,
     )
@@ -257,7 +257,7 @@ class CacheManager:
 
     def _init_distributed_coordination(self, app: Flask) -> None:
         """Initialize the distributed coordination backend (pub/sub, locks, streams)."""
-        from superset.async_events.cache_backend import (
+        from superset.coordination.cache_backend import (
             RedisCacheBackend,
             RedisSentinelCacheBackend,
         )
diff --git a/superset/utils/decorators.py b/superset/utils/decorators.py
index 8b23531886f..e71580413df 100644
--- a/superset/utils/decorators.py
+++ b/superset/utils/decorators.py
@@ -222,6 +222,7 @@ def on_error(
     ex: Exception,
     catches: tuple[type[Exception], ...] = (SQLAlchemyError,),
     reraise: type[Exception] | None = SQLAlchemyError,
+    preserve_message: bool = False,
 ) -> None:
     """
     Default error handler whenever any exception is caught during a SQLAlchemy nested
@@ -230,6 +231,10 @@ def on_error(
     :param ex: The source exception
     :param catches: The exception types the handler catches
     :param reraise: The exception type the handler raises after catching
+    :param preserve_message: When True, carry the underlying cause's message into the
+        reraised exception instead of its generic default, so the true reason (e.g.
+        "database is locked") reaches the caller. Only meaningful when ``reraise`` is
+        a ``SupersetException`` subclass (which accepts ``(message, exception)``).
     :raises Exception: If the exception is not swallowed
     """
 
@@ -238,6 +243,11 @@ def on_error(
             logger.exception(ex.exception)
 
         if reraise:
+            if preserve_message:
+                # Prefer the DBAPI cause (e.g. sqlite3's "database is locked") over
+                # SQLAlchemy's wrapper, which appends the offending SQL.
+                cause = getattr(ex, "orig", None) or ex
+                raise reraise(str(cause), ex) from ex
             raise reraise() from ex
     else:
         raise ex
diff --git a/tests/unit_tests/async_events/__init__.py b/tests/unit_tests/async_events/__init__.py
deleted file mode 100644
index 13a83393a91..00000000000
--- a/tests/unit_tests/async_events/__init__.py
+++ /dev/null
@@ -1,16 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
diff --git a/tests/unit_tests/charts/test_chart_data_api.py b/tests/unit_tests/charts/test_chart_data_api.py
index 036453885fb..e4e1c88432b 100644
--- a/tests/unit_tests/charts/test_chart_data_api.py
+++ b/tests/unit_tests/charts/test_chart_data_api.py
@@ -77,12 +77,20 @@ def _json_execution_result(
 
 def test_should_run_async_requires_async_mode_flag() -> None:
     """`async_mode` is opt-in: absent/false runs sync even with the flag on."""
+    from flask_caching.backends import NullCache
+
     api = ChartDataRestApi()
     qc = MagicMock()
     qc.result_format = ChartDataResultFormat.JSON
     qc.result_type = ChartDataResultType.FULL
 
-    with patch("superset.charts.data.api.is_feature_enabled", return_value=True):
+    with (
+        patch("superset.charts.data.api.is_feature_enabled", return_value=True),
+        patch("superset.charts.data.api.cache_manager") as cache_manager,
+    ):
+        # A real (non-null) DATA cache backend so async is viable.
+        cache_manager.data_cache.cache = MagicMock()
+
         # Opted in → async.
         assert api._should_run_async({"async_mode": True}, qc, 300) is True
         # Absent or false → synchronous (programmatic clients keep the 200 flow).
@@ -94,6 +102,10 @@ def test_should_run_async_requires_async_mode_flag() -> None:
         )
         assert cache_disabled is False
 
+        # NullCache DATA backend → sync: async could never read the result back.
+        cache_manager.data_cache.cache = NullCache()
+        assert api._should_run_async({"async_mode": True}, qc, 300) is False
+
     # Feature flag off → always sync.
     with patch("superset.charts.data.api.is_feature_enabled", return_value=False):
         assert api._should_run_async({"async_mode": True}, qc, 300) is False
diff --git a/tests/unit_tests/async_events/test_cache_backend.py b/tests/unit_tests/coordination/test_cache_backend.py
similarity index 81%
rename from tests/unit_tests/async_events/test_cache_backend.py
rename to tests/unit_tests/coordination/test_cache_backend.py
index ece81be0df6..bbe27906721 100644
--- a/tests/unit_tests/async_events/test_cache_backend.py
+++ b/tests/unit_tests/coordination/test_cache_backend.py
@@ -32,9 +32,9 @@ def test_redis_cache_backend_pins_protocol_and_timeout_defaults(
     mocker: MockerFixture,
 ) -> None:
     """RedisCacheBackend must default to RESP2 and no socket timeout."""
-    from superset.async_events.cache_backend import RedisCacheBackend
+    from superset.coordination.cache_backend import RedisCacheBackend
 
-    redis_mock = mocker.patch("superset.async_events.cache_backend.redis")
+    redis_mock = mocker.patch("superset.coordination.cache_backend.redis")
 
     RedisCacheBackend(host="localhost", port=6379)
 
@@ -49,9 +49,9 @@ def test_redis_cache_backend_allows_explicit_timeout_override(
     mocker: MockerFixture,
 ) -> None:
     """Callers can still opt into an explicit timeout if they want one."""
-    from superset.async_events.cache_backend import RedisCacheBackend
+    from superset.coordination.cache_backend import RedisCacheBackend
 
-    redis_mock = mocker.patch("superset.async_events.cache_backend.redis")
+    redis_mock = mocker.patch("superset.coordination.cache_backend.redis")
 
     RedisCacheBackend(
         host="localhost",
@@ -70,9 +70,9 @@ def test_redis_cache_backend_from_config_reads_timeout_keys(
     mocker: MockerFixture,
 ) -> None:
     """from_config should surface the new CACHE_REDIS_SOCKET_* keys."""
-    from superset.async_events.cache_backend import RedisCacheBackend
+    from superset.coordination.cache_backend import RedisCacheBackend
 
-    redis_mock = mocker.patch("superset.async_events.cache_backend.redis")
+    redis_mock = mocker.patch("superset.coordination.cache_backend.redis")
 
     RedisCacheBackend.from_config(
         {
@@ -94,9 +94,9 @@ def test_redis_sentinel_cache_backend_pins_protocol_and_timeout_defaults(
 ) -> None:
     """RedisSentinelCacheBackend must default to RESP2/no timeout on both
     the sentinel-node connections and the master data connection."""
-    from superset.async_events.cache_backend import RedisSentinelCacheBackend
+    from superset.coordination.cache_backend import RedisSentinelCacheBackend
 
-    sentinel_mock = mocker.patch("superset.async_events.cache_backend.Sentinel")
+    sentinel_mock = mocker.patch("superset.coordination.cache_backend.Sentinel")
     master_mock = mock.Mock()
     sentinel_mock.return_value.master_for.return_value = master_mock
 
@@ -122,9 +122,9 @@ def test_redis_sentinel_cache_backend_from_config_reads_timeout_keys(
     mocker: MockerFixture,
 ) -> None:
     """from_config should surface the new CACHE_REDIS_SOCKET_* keys."""
-    from superset.async_events.cache_backend import RedisSentinelCacheBackend
+    from superset.coordination.cache_backend import RedisSentinelCacheBackend
 
-    sentinel_mock = mocker.patch("superset.async_events.cache_backend.Sentinel")
+    sentinel_mock = mocker.patch("superset.coordination.cache_backend.Sentinel")
     master_mock = mock.Mock()
     sentinel_mock.return_value.master_for.return_value = master_mock
 
@@ -150,9 +150,9 @@ def test_redis_sentinel_cache_backend_from_config_reads_timeout_keys(
 
 def test_redis_cache_backend_stream_helpers(mocker: MockerFixture) -> None:
     """xread / stream_last_id / expire delegate to the redis client correctly."""
-    from superset.async_events.cache_backend import RedisCacheBackend
+    from superset.coordination.cache_backend import RedisCacheBackend
 
-    redis_mock = mocker.patch("superset.async_events.cache_backend.redis")
+    redis_mock = mocker.patch("superset.coordination.cache_backend.redis")
     client = redis_mock.Redis.return_value
     backend = RedisCacheBackend(host="localhost", port=6379)
 
@@ -174,12 +174,20 @@ def test_redis_cache_backend_stream_helpers(mocker: MockerFixture) -> None:
     assert backend.expire("s", 60) is True
     client.expire.assert_called_once_with("s", 60)
 
+    # compare_and_delete runs the Lua script (1 key + expected value), int reply
+    client.eval.return_value = 1
+    assert backend.compare_and_delete("lock", "tok") == 1
+    eval_args = client.eval.call_args.args
+    assert eval_args[1:] == (1, "lock", "tok")  # numkeys, KEYS[1], ARGV[1]
+    client.eval.return_value = 0
+    assert backend.compare_and_delete("lock", "other") == 0
+
 
 def test_redis_sentinel_cache_backend_stream_helpers(mocker: MockerFixture) -> None:
     """Sentinel backend routes the new stream helpers through its master client."""
-    from superset.async_events.cache_backend import RedisSentinelCacheBackend
+    from superset.coordination.cache_backend import RedisSentinelCacheBackend
 
-    sentinel_mock = mocker.patch("superset.async_events.cache_backend.Sentinel")
+    sentinel_mock = mocker.patch("superset.coordination.cache_backend.Sentinel")
     master = mock.Mock()
     sentinel_mock.return_value.master_for.return_value = master
     backend = RedisSentinelCacheBackend(
@@ -200,3 +208,10 @@ def test_redis_sentinel_cache_backend_stream_helpers(mocker: MockerFixture) -> N
     master.expire.return_value = 1
     assert backend.expire("s", 60) is True
     master.expire.assert_called_once_with("s", 60)
+
+    master.eval.return_value = 1
+    assert backend.compare_and_delete("lock", "tok") == 1
+    eval_args = master.eval.call_args.args
+    assert eval_args[1:] == (1, "lock", "tok")  # numkeys, KEYS[1], ARGV[1]
+    master.eval.return_value = 0
+    assert backend.compare_and_delete("lock", "other") == 0
diff --git a/tests/unit_tests/distributed_lock/distributed_lock_tests.py b/tests/unit_tests/distributed_lock/distributed_lock_tests.py
index fc675c66791..4bd0af0639e 100644
--- a/tests/unit_tests/distributed_lock/distributed_lock_tests.py
+++ b/tests/unit_tests/distributed_lock/distributed_lock_tests.py
@@ -41,6 +41,7 @@ BACKEND_DEFINED = "superset.coordination.base.CoordinationService.is_backend_def
 COORD_SET = "superset.coordination.base.CoordinationService.set_value"
 COORD_GET = "superset.coordination.base.CoordinationService.get_value"
 COORD_DELETE = "superset.coordination.base.CoordinationService.delete_value"
+COORD_CAD = "superset.coordination.base.CoordinationService.compare_and_delete"
 
 
 def _get_lock(key: UUID, session: Session) -> Any:
@@ -69,10 +70,10 @@ def _get_other_session() -> Session:
 
 
 def _fake_coordination_backend() -> dict[str, Any]:
-    """Patchable set/get/delete_value closures over a shared in-memory store.
+    """Patchable set/get/delete/compare-and-delete closures over one store.
 
-    Faithfully models SET NX + GET + DELETE so the ownership-checked release
-    (get-then-compare-then-delete) exercises end to end under mocking.
+    Faithfully models SET NX + GET + DELETE + atomic compare-and-delete so the
+    ownership-checked release exercises end to end under mocking.
     """
     store: dict[str, Any] = {}
 
@@ -95,7 +96,20 @@ def _fake_coordination_backend() -> dict[str, Any]:
     def _delete(*keys: str, backend: Any = None) -> int:
         return sum(1 for k in keys if store.pop(k, None) is not None)
 
-    return {"store": store, "set": _set, "get": _get, "delete": _delete}
+    def _compare_and_delete(key: str, expected: str, backend: Any = None) -> int:
+        # Atomic: delete only if the stored value still equals ``expected``.
+        if store.get(key) == expected:
+            store.pop(key, None)
+            return 1
+        return 0
+
+    return {
+        "store": store,
+        "set": _set,
+        "get": _get,
+        "delete": _delete,
+        "compare_and_delete": _compare_and_delete,
+    }
 
 
 def test_distributed_lock_kv_happy_path() -> None:
@@ -178,7 +192,8 @@ def test_distributed_lock_uses_redis_when_configured() -> None:
         patch(BACKEND_DEFINED, return_value=True),
         patch(COORD_SET, side_effect=fake["set"]) as mock_set,
         patch(COORD_GET, side_effect=fake["get"]),
-        patch(COORD_DELETE, side_effect=fake["delete"]) as mock_delete,
+        patch(COORD_DELETE, side_effect=fake["delete"]),
+        patch(COORD_CAD, side_effect=fake["compare_and_delete"]) as mock_cad,
     ):
         with DistributedLock("test_redis", key="value") as lock_key:
             assert lock_key is not None
@@ -188,9 +203,9 @@ def test_distributed_lock_uses_redis_when_configured() -> None:
             assert call_args.kwargs["if_absent"] is True
             assert "ttl" in call_args.kwargs
 
-        # Verify the (ownership-checked) DELETE was called on exit
-        mock_delete.assert_called_once()
-        # And the lock is actually gone.
+        # Verify the ownership-checked (atomic compare-and-delete) release ran on
+        # exit and the lock is actually gone.
+        mock_cad.assert_called_once()
         assert fake["store"] == {}
 
 
@@ -203,7 +218,8 @@ def test_distributed_lock_redis_release_only_deletes_own_lock() -> None:
         patch(BACKEND_DEFINED, return_value=True),
         patch(COORD_SET, side_effect=fake["set"]),
         patch(COORD_GET, side_effect=fake["get"]),
-        patch(COORD_DELETE, side_effect=fake["delete"]) as mock_delete,
+        patch(COORD_DELETE, side_effect=fake["delete"]),
+        patch(COORD_CAD, side_effect=fake["compare_and_delete"]),
     ):
         # Holder B owns the key.
         with DistributedLock("test_redis", key="value"):
@@ -216,8 +232,7 @@ def test_distributed_lock_redis_release_only_deletes_own_lock() -> None:
                 token="stale-token-a",  # noqa: S106
             ).run()
 
-            # A's release deleted nothing; B still holds the key.
-            mock_delete.assert_not_called()
+            # A's compare-and-delete matched nothing; B still holds the key.
             assert next(iter(fake["store"].values())) == b_token
 
 
@@ -253,6 +268,7 @@ def test_distributed_lock_custom_ttl() -> None:
         patch(COORD_SET, side_effect=fake["set"]) as mock_set,
         patch(COORD_GET, side_effect=fake["get"]),
         patch(COORD_DELETE, side_effect=fake["delete"]),
+        patch(COORD_CAD, side_effect=fake["compare_and_delete"]),
     ):
         with DistributedLock("test", ttl_seconds=60, key="value"):
             call_args = mock_set.call_args
@@ -269,6 +285,7 @@ def test_distributed_lock_default_ttl(app_context: None) -> None:
         patch(COORD_SET, side_effect=fake["set"]) as mock_set,
         patch(COORD_GET, side_effect=fake["get"]),
         patch(COORD_DELETE, side_effect=fake["delete"]),
+        patch(COORD_CAD, side_effect=fake["compare_and_delete"]),
     ):
         with DistributedLock("test", key="value"):
             call_args = mock_set.call_args
diff --git a/tests/unit_tests/tasks/test_dependencies.py b/tests/unit_tests/tasks/test_dependencies.py
index d817ee1f688..9a8ce038eea 100644
--- a/tests/unit_tests/tasks/test_dependencies.py
+++ b/tests/unit_tests/tasks/test_dependencies.py
@@ -98,6 +98,63 @@ class TestResolveFailedPrerequisite:
         assert _resolve_failed_prerequisite(task) is prerequisite
 
 
+class TestExecuteTaskFailedPrerequisite:
+    """execute_task's handling when a prerequisite did not succeed."""
+
+    @staticmethod
+    def _run_with_transition(*, transitioned: bool, refreshed_status: str):
+        """Drive execute_task through the failed-prerequisite branch.
+
+        Returns ``(result, publish_completion_mock)`` with the status transition
+        forced to ``transitioned`` and the post-transition re-read reporting
+        ``refreshed_status``.
+        """
+        native = uuid4()
+        pending = SimpleNamespace(uuid=native, status=TaskStatus.PENDING.value)
+        refreshed = SimpleNamespace(uuid=native, status=refreshed_status)
+        failed_prereq = _task(status=TaskStatus.FAILURE.value)
+
+        transition = MagicMock()
+        transition.return_value.run.return_value = transitioned
+
+        with (
+            patch(
+                "superset.tasks.scheduler.TaskDAO.find_one_or_none",
+                side_effect=[pending, refreshed],
+            ),
+            patch(
+                "superset.tasks.scheduler._resolve_failed_prerequisite",
+                return_value=failed_prereq,
+            ),
+            patch("superset.tasks.scheduler.TaskManager") as task_manager,
+            patch(
+                "superset.commands.tasks.internal_update."
+                "InternalStatusTransitionCommand",
+                transition,
+            ),
+        ):
+            from superset.tasks.scheduler import execute_task
+
+            result = execute_task.run(str(native), "some.task", (), {})
+            return result, task_manager.publish_completion
+
+    def test_failure_published_when_transition_commits(self):
+        result, publish_completion = self._run_with_transition(
+            transitioned=True, refreshed_status=TaskStatus.PENDING.value
+        )
+        assert result["status"] == TaskStatus.FAILURE.value
+        publish_completion.assert_called_once()
+
+    def test_no_failure_published_when_task_already_terminal(self):
+        # The task was aborted while waiting on prerequisites, so the FAILURE
+        # transition is a no-op — report the committed status, publish no FAILURE.
+        result, publish_completion = self._run_with_transition(
+            transitioned=False, refreshed_status=TaskStatus.ABORTED.value
+        )
+        assert result["status"] == TaskStatus.ABORTED.value
+        publish_completion.assert_not_called()
+
+
 class TestPersistDependencies:
     """Tests for SubmitTaskCommand._persist_dependencies."""
 
diff --git a/tests/unit_tests/utils/test_decorators.py b/tests/unit_tests/utils/test_decorators.py
index a650c6ee467..530077e5ab5 100644
--- a/tests/unit_tests/utils/test_decorators.py
+++ b/tests/unit_tests/utils/test_decorators.py
@@ -400,3 +400,32 @@ def test_transacation_nested(mocker: MockerFixture) -> None:
         nested()
     db.session.commit.assert_not_called()
     db.session.rollback.assert_called_once()
+
+
+def test_on_error_reraises_generic_message_by_default() -> None:
+    """Without ``preserve_message`` the reraised type keeps its own default message."""
+    from sqlalchemy.exc import OperationalError
+
+    from superset.commands.tasks.exceptions import TaskUpdateFailedError
+
+    source = OperationalError("UPDATE tasks ...", {}, Exception("database is locked"))
+    with pytest.raises(TaskUpdateFailedError) as excinfo:
+        decorators.on_error(source, reraise=TaskUpdateFailedError)
+    assert excinfo.value.message == "Task could not be updated."
+
+
+def test_on_error_preserves_underlying_cause_message() -> None:
+    """``preserve_message`` surfaces the DBAPI cause (e.g. "database is locked")."""
+    from sqlalchemy.exc import OperationalError
+
+    from superset.commands.tasks.exceptions import TaskUpdateFailedError
+
+    # SQLAlchemy wraps the DBAPI error and exposes it as ``.orig``; the wrapper's
+    # own str would append the SQL, so the DBAPI cause is the accurate message.
+    source = OperationalError("UPDATE tasks ...", {}, Exception("database is locked"))
+    with pytest.raises(TaskUpdateFailedError) as excinfo:
+        decorators.on_error(
+            source, reraise=TaskUpdateFailedError, preserve_message=True
+        )
+    assert "database is locked" in excinfo.value.message
+    assert excinfo.value.exception is source