hermes-agent/apps/desktop/electron/main-process-lag-watchdog.ts
brooklyn! da77a7e2e8 fix(desktop): defer tray hide and log main-process stalls
Hiding inside the minimize handler wedges the Windows minimized flag, and
showInactive() restores a painted window that never takes input. Defer hide
until after that dispatch, skip a stale hide if the user already restored,
and activate with show()+focus() on Windows. Log main-process event-loop
stalls in desktop.log; renderer unresponsive handlers cannot see AppHangB1.

Reported in #119252.

Co-authored-by: finn763 <[email protected]>
Co-authored-by: KoNit-K <[email protected]>
2026-09-24 19:53:53 -05:00

65 lines
1.5 KiB
TypeScript

type IntervalHandle = ReturnType<typeof setInterval>
interface MainProcessLagWatchdogOptions {
cadenceMs: number
thresholdMs: number
now: () => number
log: (message: string) => void
setInterval: (callback: () => void, delayMs: number) => IntervalHandle
clearInterval: (timer: IntervalHandle) => void
}
/**
* Records delayed main-process timer callbacks after the event loop resumes.
* It intentionally observes only; a stall must never change window or tray
* behavior while the application is recovering.
*/
export function createMainProcessLagWatchdog({
cadenceMs,
thresholdMs,
now,
log,
setInterval,
clearInterval
}: MainProcessLagWatchdogOptions) {
let timer: IntervalHandle | undefined
let expectedAt = 0
const tick = () => {
if (!timer) {
return
}
const observedAt = now()
const lagMs = Math.max(0, observedAt - expectedAt)
if (lagMs >= thresholdMs) {
log(
`[diagnostics] main-process event loop lagged ${lagMs}ms (expected tick at ${expectedAt}ms, observed at ${observedAt}ms)`
)
}
// Rebase after every callback: one late tick is evidence, not a reason to
// report the same delay again on every healthy future cadence.
expectedAt = observedAt + cadenceMs
}
return {
start: () => {
if (timer) {
return
}
expectedAt = now() + cadenceMs
timer = setInterval(tick, cadenceMs)
},
stop: () => {
if (!timer) {
return
}
clearInterval(timer)
timer = undefined
}
}
}