Skip to content

Commit

Permalink
fix: Renderer ANR detection
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Feb 14, 2025
1 parent 9175622 commit 420a101
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/main/anr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { callFrameToStackFrame, Event, logger, stripSentryFramesAndReverse, watchdogTimer } from '@sentry/core';
import { captureEvent, createGetModuleFromFilename, getClient, StackFrame } from '@sentry/node';
import { app, WebContents } from 'electron';
import { app, powerMonitor, WebContents } from 'electron';

import { RendererStatus } from '../common/ipc';
import { ElectronMainOptions } from './sdk';
Expand Down Expand Up @@ -133,6 +133,14 @@ export function createRendererAnrStatusHandler(): (status: RendererStatus, conte

let watchdog = rendererWatchdogTimers.get(contents);

function disable(): void {
watchdog?.enabled(false);
}

function enable(): void {
watchdog?.enabled(true);
}

if (watchdog === undefined) {
log('Renderer sent first status message', message.config);
let pauseAndCapture: (() => void) | undefined;
Expand All @@ -158,8 +166,20 @@ export function createRendererAnrStatusHandler(): (status: RendererStatus, conte

contents.once('destroyed', () => {
rendererWatchdogTimers?.delete(contents);

powerMonitor.off('suspend', disable);
powerMonitor.off('resume', enable);
powerMonitor.off('lock-screen', disable);
powerMonitor.off('unlock-screen', enable);
});

contents.once('blur', disable);
contents.once('focus', enable);
powerMonitor.on('suspend', disable);
powerMonitor.on('resume', enable);
powerMonitor.on('lock-screen', disable);
powerMonitor.on('unlock-screen', enable);

rendererWatchdogTimers.set(contents, watchdog);
}

Expand Down
6 changes: 1 addition & 5 deletions src/renderer/anr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-globals */
import { RendererProcessAnrOptions } from '../common/ipc';
import { getIPC } from './ipc';

Expand All @@ -15,10 +14,7 @@ export function enableAnrRendererMessages(options: Partial<RendererProcessAnrOpt

const ipc = getIPC();

document.addEventListener('visibilitychange', () => {
ipc.sendStatus({ status: document.visibilityState, config });
});

// eslint-disable-next-line no-restricted-globals
ipc.sendStatus({ status: document.visibilityState, config });

setInterval(() => {
Expand Down

0 comments on commit 420a101

Please sign in to comment.