Skip to content

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

Keep a query cancelable after a nested query finishes (#84359)

Chris Truter5a930a8d0766
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
index b63d78c9cc8..80477d10e3e 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/query_processor/middleware/sandboxing.clj
@@ -213,9 +213,10 @@
(let [result
;; Rebind *result* in case the outer query is being streamed back to the client. The streaming code binds this
;; to a custom handler, and we don't want to accidentally terminate the stream here!
+ ;; This query gets its own cancellation channel too: finishing closes it, and the outer query has yet to run.
(binding [qp.pipeline/*result* qp.pipeline/default-result-handler]
(request/as-admin
- (qp/process-query query)))]
+ (qp.pipeline/do-with-own-canceled-chan #(qp/process-query query))))]
(when-not (= (:status result) :completed)
(throw (ex-info "Error running query to determine metadata"
{:query query, :result result})))
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
index b2c19dbc41a..356ce04c99d 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
@@ -27,6 +27,7 @@
[metabase.query-processor.middleware.cache-test :as cache-test]
[metabase.query-processor.middleware.permissions :as qp.perms]
[metabase.query-processor.middleware.process-userland-query-test :as process-userland-query-test]
+ [metabase.query-processor.pipeline :as qp.pipeline]
[metabase.query-processor.pivot :as qp.pivot]
[metabase.query-processor.pivot.test-util :as qp.pivot.test-util]
[metabase.query-processor.preprocess :as qp.preprocess]
@@ -298,6 +299,30 @@
(run-venues-count-query)))
(fails-without-token (run-venues-count-query))))))
+(deftest native-sandbox-metadata-query-keeps-outer-query-cancelable-test
+ (testing "The query that fetches a native sandbox's metadata leaves the outer query cancelable"
+ (met/with-gtaps! {:gtaps {:venues (venues-category-native-sandbox-def)}, :attributes {"cat" 50}}
+ (let [canceled-chan (a/promise-chan)
+ execute qp.pipeline/*execute*
+ calls (atom 0)
+ driver-saw (atom nil)]
+ (binding [qp.pipeline/*canceled-chan* canceled-chan
+ qp.pipeline/*execute* (fn [driver query respond]
+ (if (= 1 (swap! calls inc))
+ (execute driver query respond)
+ ;; The outer query: cancel the request, and see whether its driver hears it.
+ (do
+ (a/>!! canceled-chan ::canceled)
+ (reset! driver-saw (a/poll! qp.pipeline/*canceled-chan*))
+ nil)))]
+ ;; The canceled outer query returns no rows, which the row helpers reject.
+ (try
+ (run-venues-count-query)
+ (catch Exception _)))
+ (testing "the metadata query runs first, then the outer query, which hears the cancel"
+ (is (= 2 @calls))
+ (is (= ::canceled @driver-saw)))))))
+
(deftest metric-on-natively-sandboxed-table-test
(testing "A :metric reference on a natively-sandboxed table should succeed (#76044)"
(met/with-gtaps! {:gtaps {:venues (venues-category-native-sandbox-def)}, :attributes {"cat" 50}}
diff --git a/src/metabase/query_processor/pipeline.clj b/src/metabase/query_processor/pipeline.clj
index 51996488fbf..ad5f975915c 100644
--- a/src/metabase/query_processor/pipeline.clj
+++ b/src/metabase/query_processor/pipeline.clj
@@ -36,6 +36,38 @@
[]
(canceled?* *canceled-chan*))
+(defn do-with-own-canceled-chan
+ "Call `thunk` so that the query it runs cannot close a cancellation channel another query still needs.
+
+ Use this for a query that runs while another is in progress, or for each of several queries one channel cancels.
+
+ The outer channel is `outer-chan`, or the current [[*canceled-chan*]] when none is passed.
+ - If there is one, `thunk` runs with [[*canceled-chan*]] bound to a new channel, canceled whenever the outer one is.
+ - If there is none, there is nothing to protect, and `thunk` runs with the binding unchanged.
+
+ The outer channel must keep its value for every reader, as a promise channel does."
+ ([thunk]
+ (do-with-own-canceled-chan *canceled-chan* thunk))
+ ([outer-chan thunk]
+ ;; A query closes [[*canceled-chan*]] when it finishes.
+ ;; On a shared channel, a query still running, or a later one, would never hear it was canceled or timed out.
+ (if-not outer-chan
+ (thunk)
+ (let [own-chan (a/promise-chan)
+ done-chan (a/promise-chan)]
+ ;; A cancel that has already arrived is copied now, so the query sees it before it starts.
+ (some->> (a/poll! outer-chan) (a/offer! own-chan))
+ (a/go
+ ;; Waits for a cancel on `outer-chan` until the `finally` below closes `done-chan`, which gives nil.
+ (when-some [value (first (a/alts! [outer-chan done-chan]))]
+ (a/>! own-chan value)))
+ (try
+ (binding [*canceled-chan* own-chan]
+ (thunk))
+ (finally
+ (a/close! own-chan)
+ (a/close! done-chan)))))))
+
(defn default-result-handler
"Default implementation for *result*."
[result]
diff --git a/test/metabase/query_processor/pipeline_test.clj b/test/metabase/query_processor/pipeline_test.clj
new file mode 100644
index 00000000000..7f6e53750f2
--- /dev/null
+++ b/test/metabase/query_processor/pipeline_test.clj
@@ -0,0 +1,77 @@
+(ns metabase.query-processor.pipeline-test
+ (:require
+ [clojure.core.async :as a]
+ ;; Public core.async can neither tell whether a channel is closed nor hold back a go block's take.
+ [clojure.core.async.impl.protocols :as a.impl.protocols]
+ [clojure.test :refer :all]
+ [metabase.query-processor.pipeline :as qp.pipeline]))
+
+(defn- received
+ "The value `chan` delivers within 5 seconds, or nil."
+ [chan]
+ (first (a/alts!! [chan (a/timeout 5000)])))
+
+(defn- poll-only-chan!
+ "A channel that gives `value` to [[a/poll!]] but never to a go block waiting on it.
+ It stands in for an outer channel canceled before [[qp.pipeline/do-with-own-canceled-chan]] forwards its value."
+ [value]
+ (let [ready (doto (a/promise-chan) (a/>!! value))
+ never (a/promise-chan)]
+ (reify a.impl.protocols/ReadPort
+ (take! [_ handler]
+ (a.impl.protocols/take! (if (a.impl.protocols/blockable? handler) never ready) handler)))))
+
+(deftest ^:parallel do-with-own-canceled-chan-own-channel-test
+ (testing "the inner query runs with a channel of its own, and closing it leaves the outer query cancelable"
+ (let [outer-chan (a/promise-chan)]
+ (binding [qp.pipeline/*canceled-chan* outer-chan]
+ ;; Closing the channel is what a query does when it finishes.
+ (is (not= outer-chan (qp.pipeline/do-with-own-canceled-chan #(doto qp.pipeline/*canceled-chan* a/close!))))
+ (a/>!! outer-chan ::canceled)
+ (is (= ::canceled (a/poll! outer-chan)))))))
+
+(deftest ^:parallel do-with-own-canceled-chan-closes-own-channel-test
+ (testing "the inner channel is closed once the inner query returns or throws"
+ (let [outer-chan (a/promise-chan)
+ own-chan (atom nil)]
+ (is (a.impl.protocols/closed?
+ (qp.pipeline/do-with-own-canceled-chan outer-chan (fn [] qp.pipeline/*canceled-chan*))))
+ (is (thrown? clojure.lang.ExceptionInfo
+ (qp.pipeline/do-with-own-canceled-chan
+ outer-chan
+ (fn []
+ (reset! own-chan qp.pipeline/*canceled-chan*)
+ (throw (ex-info "boom" {}))))))
+ (is (a.impl.protocols/closed? @own-chan)))))
+
+(deftest ^:parallel do-with-own-canceled-chan-forwards-cancel-test
+ (testing "canceling the outer query cancels the inner one"
+ (let [outer-chan (a/promise-chan)]
+ (binding [qp.pipeline/*canceled-chan* outer-chan]
+ (is (= ::canceled
+ (qp.pipeline/do-with-own-canceled-chan
+ (fn []
+ (a/>!! outer-chan ::canceled)
+ (received qp.pipeline/*canceled-chan*)))))))))
+
+(deftest ^:parallel do-with-own-canceled-chan-without-outer-channel-test
+ (testing "without an outer channel, none is bound"
+ (is (nil? (qp.pipeline/do-with-own-canceled-chan (fn [] qp.pipeline/*canceled-chan*))))))
+
+(deftest ^:parallel do-with-own-canceled-chan-channel-passed-in-test
+ (testing "a channel passed in cancels the query in place of the bound one"
+ (let [request-chan (a/promise-chan)]
+ (binding [qp.pipeline/*canceled-chan* (a/promise-chan)]
+ (is (= ::canceled
+ (qp.pipeline/do-with-own-canceled-chan
+ request-chan
+ (fn []
+ (a/>!! request-chan ::canceled)
+ (received qp.pipeline/*canceled-chan*)))))))))
+
+(deftest do-with-own-canceled-chan-already-canceled-test
+ (testing "the inner query sees a cancel that arrived before it started"
+ (is (= ::canceled
+ (qp.pipeline/do-with-own-canceled-chan
+ (poll-only-chan! ::canceled)
+ (fn [] (a/poll! qp.pipeline/*canceled-chan*)))))))