diff --git a/frontend/src/app/main/errors.cljs b/frontend/src/app/main/errors.cljs index 8b12cf1500..048287203f 100644 --- a/frontend/src/app/main/errors.cljs +++ b/frontend/src/app/main/errors.cljs @@ -12,6 +12,7 @@ [app.config :as cf] [app.main.data.auth :as da] [app.main.data.event :as ev] + [app.main.data.helpers :as dsh] [app.main.data.modal :as modal] [app.main.data.nitrate :as dnt] [app.main.data.notifications :as ntf] @@ -437,36 +438,90 @@ hint occurrences)))) +(def ^:private update-loop-observe-ms + "Time the scheduler trips are counted after the first one, so the report + tells a loop that goes on (many trips) from a long chain of updates that + ends (one trip)." + 5000) + +(def ^:private update-loop-stack-lines + "Lines kept from the stack of the last trip." + 25) + (defonce ^:private update-loop-warned-at (volatile! 0)) +;; The trips counted since the first one of the current report, or nil +(defonce ^:private update-loop-run + (volatile! nil)) + +(defn- current-stack + [] + (.-stack (js/Error.))) + +(defn- stack-head + [stack] + (some->> (str/lines stack) + (take update-loop-stack-lines) + (str/join "\n"))) + +(defn- report-update-loop + [] + (try + (when-let [{:keys [rounds trips stack last-stack events objects]} @update-loop-run] + (vreset! update-loop-run nil) + (let [cause (ex-info "update loop throttled" + (cond-> {:type :internal + :code :update-loop-throttled + :rounds rounds + :trips trips + :observed-ms update-loop-observe-ms} + (some? objects) + (assoc :page-objects objects) + + (some? last-stack) + (assoc :last-trip-stack (stack-head last-stack))))] + ;; The trace must show where the first trip happened, not this + ;; delayed call. + (set! (.-stack cause) stack) + (when (seq events) + (.set in-flight-by-cause cause (in-flight-lines events))) + (submit-report :event-name "unhandled-exception" + :hint (ex-message cause) + :report (fn [] (generate-report cause)) + :cause cause))) + (catch :default _ + nil))) + (defn on-update-loop "Handler for the rumext update scheduler trips: logs the throttled update loop and reports it as an unhandled exception with the events - being processed, at most once per `report-window-ms`. Runs inside store - watchers and effect bodies, so it never throws." + being processed, at most once per `report-window-ms`. The report waits + `update-loop-observe-ms` and counts the trips meanwhile. Runs inside + store watchers and effect bodies, so it never throws." [{:keys [rounds]}] (try - (let [now (inst-ms (ct/now))] - (when (>= (- now @update-loop-warned-at) report-window-ms) - (vreset! update-loop-warned-at now) - (let [events (ptk/in-flight-events st/state) - cause (ex-info "update loop throttled" - {:type :internal - :code :update-loop-throttled - :rounds rounds})] - (.warn js/console "[update-scheduler] update loop throttled; in-flight events:" - (into-array (map ptk/repr-event events))) - (when (seq events) - (.set in-flight-by-cause cause (in-flight-lines events))) - ;; Report outside the loop that tripped the scheduler, as an - ;; unhandled exception: a throttled loop is an application defect, - ;; so it must reach the error reports and alerts, not only audit. - (ts/schedule - #(submit-report :event-name "unhandled-exception" - :hint (ex-message cause) - :report (fn [] (generate-report cause)) - :cause cause))))) + (if (some? @update-loop-run) + (vswap! update-loop-run #(-> % + (update :trips inc) + (assoc :last-stack (current-stack)))) + (let [now (inst-ms (ct/now))] + (when (>= (- now @update-loop-warned-at) report-window-ms) + (vreset! update-loop-warned-at now) + (let [events (ptk/in-flight-events st/state)] + (.warn js/console "[update-scheduler] update loop throttled; in-flight events:" + (into-array (map ptk/repr-event events))) + (vreset! update-loop-run + {:rounds rounds + :trips 1 + :stack (current-stack) + :events events + :objects (some-> (dsh/lookup-page-objects @st/state) count)}) + ;; Report outside the loop that tripped the scheduler, as an + ;; unhandled exception: a throttled loop is an application + ;; defect, so it must reach the error reports and alerts, not + ;; only audit. + (ts/schedule update-loop-observe-ms report-update-loop))))) (catch :default _ nil))) diff --git a/frontend/test/frontend_tests/main_errors_test.cljs b/frontend/test/frontend_tests/main_errors_test.cljs index e8b994abb9..f96a1f0384 100644 --- a/frontend/test/frontend_tests/main_errors_test.cljs +++ b/frontend/test/frontend_tests/main_errors_test.cljs @@ -558,6 +558,7 @@ (t/deftest ^:async update-loop-is-reported-with-the-events-in-flight (errors/reset-report-governor!) (vreset! @#'errors/update-loop-warned-at 0) + (vreset! @#'errors/update-loop-run nil) (let [events (atom []) loop-ev (ptk/reify ::test-update-loop-source)] (await @@ -566,7 +567,7 @@ ptk/in-flight-events (fn [_] [loop-ev]) st/format-last-events (mock/stub (fn [& _] "(stub last events)")) rt/get-current-href (constantly "https://penpot.example.com/#/workspace") - tm/schedule (mock/stub (fn [f] (f)))} + tm/schedule (mock/stub (fn [_ f] (f)))} (errors/on-update-loop {:rounds 26}) ;; A second trip within the window is not reported again (errors/on-update-loop {:rounds 26}) @@ -579,6 +580,31 @@ (t/is (str/includes? (in-flight-section (:report props)) "test-update-loop-source"))))))) +(t/deftest ^:async update-loop-report-counts-the-trips + ;; A loop that goes on trips the scheduler again and again; the report + ;; waits a few seconds and tells how many times. + (errors/reset-report-governor!) + (vreset! @#'errors/update-loop-warned-at 0) + (vreset! @#'errors/update-loop-run nil) + (let [events (atom []) + scheduled (atom nil)] + (await + (mock/with-mocks* + {st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) + ptk/in-flight-events (fn [_] []) + st/format-last-events (mock/stub (fn [& _] "(stub last events)")) + rt/get-current-href (constantly "https://penpot.example.com/#/workspace") + tm/schedule (mock/stub (fn [_ f] (reset! scheduled f)))} + (errors/on-update-loop {:rounds 26}) + (errors/on-update-loop {:rounds 26}) + (errors/on-update-loop {:rounds 26}) + (t/is (empty? @events) "nothing is reported while the trips are counted") + (@scheduled) + (t/is (= 1 (count @events))) + (let [report (:report (deref (first @events)))] + (t/is (str/includes? report ":trips 3")) + (t/is (str/includes? report ":last-trip-stack"))))))) + ;; --------------------------------------------------------------------------- ;; Expired organization SSO session ;;