Skip to content

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

Fix OOM with json/decode (#76221)

Alexander Polyankinb342ae4aa754
diff --git a/src/metabase/util/json.clj b/src/metabase/util/json.clj
index 48337d19d0e..a3218a3b151 100644
--- a/src/metabase/util/json.clj
+++ b/src/metabase/util/json.clj
@@ -74,10 +74,16 @@
(cheshire/generate-stream obj writer opt-map))
(defn decode
- "Decode a value from a JSON from a string, InputStream, or Reader."
+ "Decode a value from a JSON from a string, InputStream, or Reader.
+
+ For strings this uses `cheshire/parse-string-strict`, NOT `parse-string`: for a top-level JSON array `parse-string`
+ returns a *lazy* seq that retains the Jackson parser (and its ~8KB read buffers) until fully realized -- so e.g.
+ every Field's `nfc_path` (a JSON array) would pin a parser, and a wide table's worth of them costs >1GB. The strict
+ variant realizes the value and releases the parser; laziness buys nothing here anyway since the source string is
+ already fully in memory."
([source] (decode source nil))
([source key-fn]
- (cond (string? source) (cheshire/parse-string source key-fn)
+ (cond (string? source) (cheshire/parse-string-strict source key-fn)
(instance? Reader source) (cheshire/parse-stream source key-fn)
(instance? InputStream source) (with-open [r (io/reader source)]
(cheshire/parse-stream r key-fn))
diff --git a/test/metabase/util/json_test.clj b/test/metabase/util/json_test.clj
new file mode 100644
index 00000000000..cd8aa89a34c
--- /dev/null
+++ b/test/metabase/util/json_test.clj
@@ -0,0 +1,11 @@
+(ns metabase.util.json-test
+ (:require
+ [clojure.test :refer [deftest is]]
+ [metabase.util.json :as json]))
+
+(set! *warn-on-reflection* true)
+
+(deftest ^:parallel decode-does-not-retain-jackson-parser-test
+ (let [result (json/decode "[1]")]
+ (is (vector? result))
+ (is (= [1] result))))