Skip to content

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

fix: cancel serialization when client disconnects (#69864)

Edward Pagetdd34d56120a4
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index ff11a69954a..64ac7d86fa9 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2518,6 +2518,7 @@
plugins
premium-features
search
+ server
setup
util}}
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/api.clj b/enterprise/backend/src/metabase_enterprise/serialization/api.clj
index 318bb32fc4a..fbc5aea69e8 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/api.clj
@@ -1,5 +1,6 @@
(ns metabase-enterprise.serialization.api
(:require
+ [clojure.core.async :as a]
[clojure.java.io :as io]
[clojure.string :as str]
[java-time.api :as t]
@@ -14,9 +15,11 @@
[metabase.appearance.core :as appearance]
[metabase.logger.core :as logger]
[metabase.models.serialization :as serdes]
+ [metabase.server.streaming-response :as sr]
[metabase.util :as u]
[metabase.util.compress :as u.compress]
[metabase.util.date-2 :as u.date]
+ [metabase.util.jvm :as u.jvm]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -156,6 +159,32 @@
:callback #(when (.exists dst)
(run! io/delete-file (reverse (file-seq dst))))}))
+(defn- serialization-api-handler
+ [collection result-atom opts]
+ (let [done-chan (a/chan)]
+ [(u.jvm/in-virtual-thread*
+ (let [start (System/nanoTime)
+ {:keys [archive report error-message] :as pack-result} (serialize&pack opts)]
+ (analytics/track-event! :snowplow/serialization
+ {:event :serialization
+ :direction "export"
+ :source "api"
+ :duration_ms (int (/ (- (System/nanoTime) start) 1e6))
+ :count (count (:seen report))
+ :error_count (count (:errors report))
+ :collection (str/join "," (map str collection))
+ :all_collections (and (empty? collection)
+ (not (:no-collections opts)))
+ :data_model (not (:no-data-model opts))
+ :settings (not (:no-settings opts))
+ :field_values (:include-field-values opts)
+ :secrets (:include-database-secrets opts)
+ :success (boolean archive)
+ :error_message error-message})
+ (reset! result-atom pack-result)
+ (a/>!! done-chan :done)))
+ done-chan]))
+
;;; HTTP API
;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
@@ -202,8 +231,7 @@
[:continue_on_error {:default false} (mu/with ms/BooleanValue {:description "Do not break execution on errors"})]
[:full_stacktrace {:default false} (mu/with ms/BooleanValue {:description "Show full stacktraces in the logs"})]]]
(api/check-superuser)
- (let [start (System/nanoTime)
- opts {:targets (mapv #(vector "Collection" %)
+ (let [opts {:targets (mapv #(vector "Collection" %)
collection)
:no-collections (and (empty? collection)
(not all-collections?))
@@ -214,36 +242,30 @@
:dirname dirname
:continue-on-error continue-on-error?
:full-stacktrace full-stacktrace?}
- {:keys [archive
- log-file
- report
- status
- error-message
- callback]} (serialize&pack opts)]
- (analytics/track-event! :snowplow/serialization
- {:event :serialization
- :direction "export"
- :source "api"
- :duration_ms (int (/ (- (System/nanoTime) start) 1e6))
- :count (count (:seen report))
- :error_count (count (:errors report))
- :collection (str/join "," (map str collection))
- :all_collections (and (empty? collection)
- (not (:no-collections opts)))
- :data_model (not (:no-data-model opts))
- :settings (not (:no-settings opts))
- :field_values (:include-field-values opts)
- :secrets (:include-database-secrets opts)
- :success (boolean archive)
- :error_message error-message})
- (if archive
- {:status 200
- :headers {"Content-Type" "application/gzip"
- "Content-Disposition" (format "attachment; filename=\"%s\"" (.getName ^File archive))}
- :body (on-response! archive callback)}
- {:status (or status 500)
- :headers {"Content-Type" "text/plain"}
- :body (on-response! log-file callback)})))
+ result (atom nil)
+ [archive-thread finished-chan] (serialization-api-handler collection result opts)]
+ (sr/streaming-response {:content-type "application/gzip" :status 200} [os cancel-chan]
+ (a/<!!
+ (a/go
+ (case (a/alt! cancel-chan :cancel finished-chan :done)
+ :cancel (do
+ (future-cancel archive-thread)
+ (when-let [cb (:callback @result)]
+ (cb))
+ (.close os))
+ :done (let [{:keys [archive status log-file callback]} @result]
+ (try
+ (if archive
+ (do
+ (sr/set-header! "Content-Disposition"
+ (format "attachment; filename=\"%s\"" (.getName ^File archive)))
+ (io/copy archive os))
+ (do
+ (sr/set-status! (or status 500))
+ (sr/set-content-type! "text/plain")
+ (io/copy log-file os)))
+ (finally
+ (callback))))))))))
;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
;; of the REST API
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
index 13f00a60a06..26fbcceed86 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/api_test.clj
@@ -412,7 +412,7 @@
:collection (:id coll) :data_model false :settings false
:full_stacktrace true)
log (slurp (io/input-stream res))]
- (is (< 200 (count (str/split-lines log))))
+ (is (< 50 (count (str/split-lines log))))
;; Pop out the error event
(snowplow-test/pop-event-data-and-user-id!))))))))
diff --git a/src/metabase/server/streaming_response.clj b/src/metabase/server/streaming_response.clj
index 69d87659891..401c1cdaa35 100644
--- a/src/metabase/server/streaming_response.clj
+++ b/src/metabase/server/streaming_response.clj
@@ -425,7 +425,6 @@
(write-something-to-stream! os))
`f` should block until it is completely finished writing to the stream, which will be closed thereafter.
- NOTE: `canceled-chan` **IS NOT WORKING**; see `metabase.server.streaming-response-test/canceling-response-2`
`canceled-chan` can be monitored to see if the request is canceled before results are fully written to the stream.
Inside the body, [[*response*]] is bound to the `HttpServletResponse`. You can use the helper functions