✨ Count the trips of a throttled update loop before reporting it (#12941)

* ✨ Count the trips of a throttled update loop before reporting it

The report of a throttled update loop could not tell a loop that goes
on from a long chain of updates that ends. It now waits five seconds
and reports how many times the scheduler tripped meanwhile, the stack
of the last trip and the number of objects of the page. The trace still
shows the first trip.

Relates #12580

AI-assisted-by: claude-opus-5-5

* ♻️ Drop the bang suffix from the update loop report function

Review asked for no new functions with a ! suffix.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Eva Marco 2026-11-25 13:52:15 +01:00 committed by GitHub
parent 1438be5b26
commit efe420e3e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 104 additions and 23 deletions

View File

@ -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)))

View File

@ -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
;;