Skip to content

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

Fix typo in self-dependency removal GHY-3198 (#70535)

Eric Normand7c8125d4bb96
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/core_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/core_test.clj
index 8153a078c09..aa6fdb25899 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/core_test.clj
@@ -221,3 +221,22 @@
:graph graph)]
;; Because we are changing a snippet, don't check anything downstream
(is (= {} errors)))))))
+
+(deftest ^:parallel self-referential-dependency-test
+ (testing "errors-from-proposed-edits terminates when the dependency graph has a self-loop (#70452)"
+ (let [{:keys [provider mbql-base]} (testbed)
+ ;; Graph where dashboard 999 depends on card 101, and dashboard 999 depends on itself
+ ;; (as happens with click behavior linking to another tab on the same dashboard)
+ graph (graph/in-memory {[:card 101] #{[:dashboard 999]}
+ [:dashboard 999] #{[:dashboard 999]}})
+ card' (-> mbql-base
+ (update :dataset-query lib/filter (lib/> (meta/field-metadata :orders :quantity) 100))
+ (dissoc :result-metadata))
+ fut (future (dependencies/errors-from-proposed-edits {:card [card']}
+ :base-provider provider
+ :graph graph))
+ result (deref fut 1000 ::timeout)]
+ (when (= result ::timeout)
+ (future-cancel fut))
+ (is (not= ::timeout result) "errors-from-proposed-edits hung (possible infinite loop)")
+ (is (= {} result)))))
diff --git a/src/metabase/graph/core.cljc b/src/metabase/graph/core.cljc
index d1bbd08fa4b..337ce1c0e2c 100644
--- a/src/metabase/graph/core.cljc
+++ b/src/metabase/graph/core.cljc
@@ -116,7 +116,7 @@
child-map {}]
(let [new-children (children-of graph to-traverse)
new-traverse (into #{}
- (comp (remove child-map) cat)
+ (comp cat (remove child-map))
(vals new-children))
new-child-map (into child-map new-children)]
(if (seq new-traverse)
diff --git a/test/metabase/graph/core_test.cljc b/test/metabase/graph/core_test.clj
similarity index 76%
rename from test/metabase/graph/core_test.cljc
rename to test/metabase/graph/core_test.clj
index ce01a971730..849e5bdaee4 100644
--- a/test/metabase/graph/core_test.cljc
+++ b/test/metabase/graph/core_test.clj
@@ -151,3 +151,43 @@
3 #{}}
(graph/children-of (graph/filtered-graph g #(<= % 3))
[1 2 3]))))))
+
+(defn- with-timeout
+ "Runs `f` in a future with a timeout. Returns the result or throws if it times out."
+ [timeout-ms f]
+ (let [fut (future (f))
+ result (deref fut timeout-ms ::timeout)]
+ (when (= result ::timeout)
+ (future-cancel fut)
+ (throw (ex-info (str "Timed out after " timeout-ms "ms (possible infinite loop)")
+ {:timeout-ms timeout-ms})))
+ result))
+
+(deftest ^:parallel transitive-children-of-self-loop-test
+ (testing "transitive-children-of terminates with a self-referential node (#70452)"
+ (let [g (graph/in-memory {:a #{:b}
+ :b #{:b}})] ;; :b depends on itself
+ (is (= {:a #{:b}
+ :b #{:b}}
+ (with-timeout 200
+ #(graph/transitive-children-of g [:a]))))))
+ (testing "transitive-children-of terminates with a cycle between two nodes"
+ (let [g (graph/in-memory {:a #{:b}
+ :b #{:c}
+ :c #{:b}})] ;; :b and :c form a cycle
+ (is (= {:a #{:b}
+ :b #{:c}
+ :c #{:b}}
+ (with-timeout 200
+ #(graph/transitive-children-of g [:a])))))))
+
+(deftest ^:parallel transitive-self-loop-test
+ (testing "transitive terminates with a self-referential node"
+ (let [g (graph/in-memory {:a #{:b}
+ :b #{:b}})]
+ (is (= [:b] (vec (graph/transitive g [:a]))))))
+ (testing "transitive terminates with a cycle between two nodes"
+ (let [g (graph/in-memory {:a #{:b}
+ :b #{:c}
+ :c #{:b}})]
+ (is (= #{:b :c} (set (graph/transitive g [:a])))))))