Skip to content

Commit 20c267e

Browse files
authored
feat(node): Add disableAnrDetectionForCallback function (#14359)
- Ref getsentry/sentry-electron#1016 ```ts disableAnrDetectionForCallback(() => dialog.openSaveDialog()); ``` Maybe this should just go in the Electron SDK? I added it for Node because it may be useful outside of Electron and at least here it has access to some private variables and types.
1 parent 1ec4c49 commit 20c267e

File tree

9 files changed

+35
-1
lines changed

9 files changed

+35
-1
lines changed

packages/astro/src/index.server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export {
1717
addRequestDataToEvent,
1818
amqplibIntegration,
1919
anrIntegration,
20+
disableAnrDetectionForCallback,
2021
captureCheckIn,
2122
captureConsoleIntegration,
2223
captureEvent,

packages/aws-serverless/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {
4949
extractRequestData,
5050
createGetModuleFromFilename,
5151
anrIntegration,
52+
disableAnrDetectionForCallback,
5253
consoleIntegration,
5354
httpIntegration,
5455
nativeNodeFetchIntegration,

packages/bun/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export {
7171
extractRequestData,
7272
createGetModuleFromFilename,
7373
anrIntegration,
74+
disableAnrDetectionForCallback,
7475
consoleIntegration,
7576
httpIntegration,
7677
nativeNodeFetchIntegration,

packages/google-cloud-serverless/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {
4949
extractRequestData,
5050
createGetModuleFromFilename,
5151
anrIntegration,
52+
disableAnrDetectionForCallback,
5253
consoleIntegration,
5354
httpIntegration,
5455
nativeNodeFetchIntegration,

packages/node/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export { localVariablesIntegration } from './integrations/local-variables';
99
export { modulesIntegration } from './integrations/modules';
1010
export { onUncaughtExceptionIntegration } from './integrations/onuncaughtexception';
1111
export { onUnhandledRejectionIntegration } from './integrations/onunhandledrejection';
12-
export { anrIntegration } from './integrations/anr';
12+
export { anrIntegration, disableAnrDetectionForCallback } from './integrations/anr';
1313

1414
export { expressIntegration, expressErrorHandler, setupExpressErrorHandler } from './integrations/tracing/express';
1515
export { fastifyIntegration, setupFastifyErrorHandler } from './integrations/tracing/fastify';

packages/node/src/integrations/anr/index.ts

+27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { types } from 'node:util';
12
import { Worker } from 'node:worker_threads';
23
import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/core';
34
import {
45
GLOBAL_OBJ,
56
defineIntegration,
7+
getClient,
68
getCurrentScope,
79
getFilenameToDebugIdMap,
810
getGlobalScope,
@@ -14,6 +16,8 @@ import { NODE_VERSION } from '../../nodeVersion';
1416
import type { NodeClient } from '../../sdk/client';
1517
import type { AnrIntegrationOptions, WorkerStartData } from './common';
1618

19+
const { isPromise } = types;
20+
1721
// This string is a placeholder that gets overwritten with the worker code.
1822
export const base64WorkerScript = '###AnrWorkerScript###';
1923

@@ -213,3 +217,26 @@ async function _startWorker(
213217
clearInterval(timer);
214218
};
215219
}
220+
221+
export function disableAnrDetectionForCallback<T>(callback: () => T): T;
222+
export function disableAnrDetectionForCallback<T>(callback: () => Promise<T>): Promise<T>;
223+
/**
224+
* Disables ANR detection for the duration of the callback
225+
*/
226+
export function disableAnrDetectionForCallback<T>(callback: () => T | Promise<T>): T | Promise<T> {
227+
const integration = getClient()?.getIntegrationByName(INTEGRATION_NAME) as AnrInternal | undefined;
228+
229+
if (!integration) {
230+
return callback();
231+
}
232+
233+
integration.stopWorker();
234+
235+
const result = callback();
236+
if (isPromise(result)) {
237+
return result.finally(() => integration.startWorker());
238+
}
239+
240+
integration.startWorker();
241+
return result;
242+
}

packages/remix/src/index.server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
addRequestDataToEvent,
2222
amqplibIntegration,
2323
anrIntegration,
24+
disableAnrDetectionForCallback,
2425
captureCheckIn,
2526
captureConsoleIntegration,
2627
captureEvent,

packages/solidstart/src/server/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
addRequestDataToEvent,
1414
amqplibIntegration,
1515
anrIntegration,
16+
disableAnrDetectionForCallback,
1617
captureCheckIn,
1718
captureConsoleIntegration,
1819
captureEvent,

packages/sveltekit/src/server/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
addRequestDataToEvent,
1414
amqplibIntegration,
1515
anrIntegration,
16+
disableAnrDetectionForCallback,
1617
captureCheckIn,
1718
captureConsoleIntegration,
1819
captureEvent,

0 commit comments

Comments
 (0)