Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Return nil when cached query is canceled (like non-cached queries) (#79625)

metamben47e50180d6d1
diff --git a/src/metabase/query_processor/middleware/cache.clj b/src/metabase/query_processor/middleware/cache.clj
index 542431a23a4..3ead65a9e3a 100644
--- a/src/metabase/query_processor/middleware/cache.clj
+++ b/src/metabase/query_processor/middleware/cache.clj
@@ -342,7 +342,6 @@
(log/trace "Running query and saving cached results (if eligible)...")
(binding [qp.pipeline/*reduce* (fn reduce'
[rff metadata rows]
- {:post [(some? %)]}
(impl/do-with-serialization
(fn [in-fn result-fn]
(binding [*in-fn* in-fn
@@ -352,7 +351,7 @@
(fn [metadata]
(save-results-xform start-time-ns metadata query-hash cache-strategy (rff metadata)))))))
-(mu/defn- run-query-with-cache :- :some
+(mu/defn- run-query-with-cache :- :any
[qp {:keys [cache-strategy middleware], :as query} :- ::qp.schema/any-query
rff :- ::qp.schema/rff]
;; Query will already have `info.hash` if it's a userland query. It's not the same hash, because this is calculated
diff --git a/test/metabase/query_processor/middleware/cache_test.clj b/test/metabase/query_processor/middleware/cache_test.clj
index edd97162513..3875ddedc55 100644
--- a/test/metabase/query_processor/middleware/cache_test.clj
+++ b/test/metabase/query_processor/middleware/cache_test.clj
@@ -313,6 +313,19 @@
(is (= :not-cached
(run-query :cache-strategy strategy)))))))))
+(deftest cancellation-mid-flight-does-not-throw-assertion-test
+ (testing (str "a cache-miss query canceled between the driver's respond callback and the reducer "
+ "returns nil silently #66655.")
+ (with-mock-cache! []
+ (let [qp (cache/maybe-return-cached-results qp.pipeline/*run*)]
+ (binding [driver.settings/*query-timeout-ms* 2000
+ qp.pipeline/*canceled-chan* (a/promise-chan)
+ qp.pipeline/*execute* (fn [_driver _query respond]
+ (a/>!! qp.pipeline/*canceled-chan* ::test-cancel)
+ (respond {} [[:toucan 1]]))]
+ (driver/with-driver :h2
+ (is (nil? (qp (test-query {}) qp.reducible/default-rff)))))))))
+
(deftest not-eligible-refresh-deletes-outdated-entry-test
(testing "when the lease winner's rerun is no longer cache-eligible (ran under min_duration_ms), the expired entry
is deleted so other processes recompute instead of serving it stale for the rest of the lease window"