Skip to content

Commit b25388b

Browse files
committed
feat(node): Add disableAnrDetectionForCallback function
1 parent 4357b44 commit b25388b

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

packages/bun/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export {
6969
extractRequestData,
7070
createGetModuleFromFilename,
7171
anrIntegration,
72+
disableAnrDetectionForCallback,
7273
consoleIntegration,
7374
httpIntegration,
7475
nativeNodeFetchIntegration,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export {
4747
extractRequestData,
4848
createGetModuleFromFilename,
4949
anrIntegration,
50+
disableAnrDetectionForCallback,
5051
consoleIntegration,
5152
httpIntegration,
5253
nativeNodeFetchIntegration,

packages/node/src/index.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import * as diagnosticsChannel from 'node:diagnostics_channel';
2+
import { types } from 'node:util';
23
import { Worker } from 'node:worker_threads';
3-
import { defineIntegration, getCurrentScope, getGlobalScope, getIsolationScope, mergeScopeData } from '@sentry/core';
4+
import {
5+
defineIntegration,
6+
getClient,
7+
getCurrentScope,
8+
getGlobalScope,
9+
getIsolationScope,
10+
mergeScopeData,
11+
} from '@sentry/core';
412
import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/types';
513
import { GLOBAL_OBJ, getFilenameToDebugIdMap, logger } from '@sentry/utils';
614
import { NODE_VERSION } from '../../nodeVersion';
715
import type { NodeClient } from '../../sdk/client';
816
import type { AnrIntegrationOptions, WorkerStartData } from './common';
917

18+
const { isPromise } = types;
19+
1020
// This string is a placeholder that gets overwritten with the worker code.
1121
export const base64WorkerScript = '###AnrWorkerScript###';
1222

@@ -219,3 +229,26 @@ async function _startWorker(
219229
clearInterval(timer);
220230
};
221231
}
232+
233+
export function disableAnrDetectionForCallback<T>(callback: () => T): T;
234+
export function disableAnrDetectionForCallback<T>(callback: () => Promise<T>): Promise<T>;
235+
/**
236+
* Disables ANR detection for the duration of the callback
237+
*/
238+
export function disableAnrDetectionForCallback<T>(callback: () => T | Promise<T>): T | Promise<T> {
239+
const integration = getClient()?.getIntegrationByName(INTEGRATION_NAME) as AnrInternal | undefined;
240+
241+
if (!integration) {
242+
return callback();
243+
}
244+
245+
integration.stopWorker();
246+
247+
const result = callback();
248+
if (isPromise(result)) {
249+
return result.finally(() => integration.startWorker());
250+
}
251+
252+
integration.startWorker();
253+
return result;
254+
}

packages/remix/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
addRequestDataToEvent,
2121
amqplibIntegration,
2222
anrIntegration,
23+
disableAnrDetectionForCallback,
2324
captureCheckIn,
2425
captureConsoleIntegration,
2526
captureEvent,

packages/solidstart/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
addRequestDataToEvent,
1212
amqplibIntegration,
1313
anrIntegration,
14+
disableAnrDetectionForCallback,
1415
captureCheckIn,
1516
captureConsoleIntegration,
1617
captureEvent,

packages/sveltekit/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
addRequestDataToEvent,
1212
amqplibIntegration,
1313
anrIntegration,
14+
disableAnrDetectionForCallback,
1415
captureCheckIn,
1516
captureConsoleIntegration,
1617
captureEvent,

0 commit comments

Comments
 (0)