Skip to content

Rate-limit the Sentry capture for renderer unresponsive events #6701

Open

Description

@Kitenite

Context

#6697 made renderer unresponsive events report to Sentry in production (previously dev-only logging, so production hangs were invisible). The PostHog alert side has a 6-hour cooldown, but the Sentry captureMessage fires on every episode. A fleet-wide bug that beachballs windows repeatedly (we have had compositor stalls do exactly this) could burn Sentry quota fast.

References

Source Who Link Date
PR follow-up @Kitenite #6697 2026-08-19

Implementation notes

Files

  • apps/desktop/src/main/windows/main.ts — the unresponsive handler calling Sentry.captureMessage

Approach

Guard the Sentry call with a once-per-hour-per-session limiter (a timestamp module-local is enough). Keep the digest counter and log line unthrottled so local diagnostics stay complete; attach the suppressed-episode count as a tag on the next allowed capture so volume is still visible.

Gotchas

  • Do not throttle the recordWindowUnresponsive() digest call; only the Sentry capture.

Activity

  1. github-actions commented on Aug 20, 2026

    @github-actions
    Contributor

    The code this ticket targets isn't on main yet, so most of this is a pre-implementation review rather than a bug diagnosis.

    Summary

    This is a follow-up hardening request from #6697: renderer unresponsive episodes now report to Sentry in production, and because each episode fires its own captureMessage, a fleet-wide stall bug (repeated compositor beachballs) could produce an unbounded event volume. The ask is to gate only the Sentry capture behind a once-per-hour-per-session limiter while leaving the local log line and the PostHog digest counter untouched, carrying the suppressed count forward as a tag.

    State of the code at b104fd0

    The changes from #6697 are not present in this checkout. What exists today:

    • apps/desktop/src/main/windows/main.ts:171-181 — the unresponsive / responsive handlers, both still inside the if (isDev) block (main.ts:149), doing nothing but log.warn / log.info.
    • No Sentry.captureMessage for unresponsive anywhere; the only two main-process captures are main.ts:359 (render-process-gone) and host-service-coordinator.ts:974 (host-service crash).
    • No recordWindowUnresponsive() or any digest module — recordWindow*, healthDigest, digest all return nothing under apps/desktop/src/main.

    So this work is sequenced strictly after #6697 lands; whoever picks it up should rebase first, and the file:line in the implementation notes will have moved (the handler has to leave the isDev block to report in production at all).

    The concern is well-founded

    Nothing in the main-process Sentry setup would absorb the burst. apps/desktop/src/main/lib/sentry.ts:16-26 sets tracesSampleRate: 0 — that governs performance traces only, not captureMessage — and there is no sampleRate, no beforeSend, and no client-side dedupe configured. (The renderer config does have a beforeSend at apps/desktop/src/renderer/lib/sentry.ts:25, but that's a different client and doesn't see main-process events.) Electron fires unresponsive once per hang episode, so a stall loop that alternates unresponsive/responsive yields one full event per cycle, per user.

    Implementation notes for whoever takes this

    Put the limiter state at module scope, not inside MainWindow(). MainWindow() is re-invoked on macOS window reopen (hence the ipcHandler singleton comment at main.ts:43-44 and the attachWindow branch at main.ts:183). A let lastCapturedAt declared inside the function would reset on every reopen, which is exactly the situation — a user reopening a window after a hang — where the throttle matters most.

    Extract it as a pure module rather than inlining. main.ts isn't unit-testable (top-level electron and localDb imports), but apps/desktop/src/main/lib/renderer-exit.ts + its test are the established pattern for exactly this: a tiny pure helper pulled out of a main.ts handler purely so it can be covered. Something like main/lib/unresponsive-report-limiter.ts exporting a function that takes an injectable now and returns { allowed: boolean; suppressedSinceLast: number }, resetting the counter when it returns allowed: true.

    Tag value must be a string — match String(details.exitCode) at main.ts:363; Sentry silently coerces/drops otherwise.

    Reproduction for the limiter itself is a straightforward bun:test in the style of renderer-exit.test.ts: feed a fake clock, assert allow → suppress ×N → allow-with-suppressedSinceLast: N → counter back to 0.

    Reproducing the underlying event end-to-end has a gotcha worth knowing: a busy-loop in the renderer (const t = Date.now(); while (Date.now() - t < 15000) {} from devtools) will fire unresponsive, but initSentry() bails when NODE_ENV !== "production" or the DSN is unset (sentry.ts:11-13), so no real capture happens in dev. Verify the throttle by stubbing/spying the capture call, not by watching a Sentry project.

    Adjacent, out of scope

    main.ts:358-366 (render-process-gone) has the same unbounded shape and would fire once per crash in a crash-respawn loop. It's rarer per session than a beachball, so probably not worth coupling to this ticket, but if the limiter lands as a reusable module it's a cheap second caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions