diff --git a/packages/astro/src/index.server.ts b/packages/astro/src/index.server.ts index 85603431379b..7eca9de9a41a 100644 --- a/packages/astro/src/index.server.ts +++ b/packages/astro/src/index.server.ts @@ -17,6 +17,7 @@ export { addRequestDataToEvent, amqplibIntegration, anrIntegration, + disableAnrDetectionForCallback, captureCheckIn, captureConsoleIntegration, captureEvent, diff --git a/packages/aws-serverless/src/index.ts b/packages/aws-serverless/src/index.ts index 6063c0c2f93d..3f167b62a7e3 100644 --- a/packages/aws-serverless/src/index.ts +++ b/packages/aws-serverless/src/index.ts @@ -49,6 +49,7 @@ export { extractRequestData, createGetModuleFromFilename, anrIntegration, + disableAnrDetectionForCallback, consoleIntegration, httpIntegration, nativeNodeFetchIntegration, diff --git a/packages/bun/src/index.ts b/packages/bun/src/index.ts index 7377d2ac3bbd..1ba5f2de4786 100644 --- a/packages/bun/src/index.ts +++ b/packages/bun/src/index.ts @@ -71,6 +71,7 @@ export { extractRequestData, createGetModuleFromFilename, anrIntegration, + disableAnrDetectionForCallback, consoleIntegration, httpIntegration, nativeNodeFetchIntegration, diff --git a/packages/google-cloud-serverless/src/index.ts b/packages/google-cloud-serverless/src/index.ts index 64d57ec530b3..6f89769c2a37 100644 --- a/packages/google-cloud-serverless/src/index.ts +++ b/packages/google-cloud-serverless/src/index.ts @@ -49,6 +49,7 @@ export { extractRequestData, createGetModuleFromFilename, anrIntegration, + disableAnrDetectionForCallback, consoleIntegration, httpIntegration, nativeNodeFetchIntegration, diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 4572cf65b9ce..fa16ac4e6b3d 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -9,7 +9,7 @@ export { localVariablesIntegration } from './integrations/local-variables'; export { modulesIntegration } from './integrations/modules'; export { onUncaughtExceptionIntegration } from './integrations/onuncaughtexception'; export { onUnhandledRejectionIntegration } from './integrations/onunhandledrejection'; -export { anrIntegration } from './integrations/anr'; +export { anrIntegration, disableAnrDetectionForCallback } from './integrations/anr'; export { expressIntegration, expressErrorHandler, setupExpressErrorHandler } from './integrations/tracing/express'; export { fastifyIntegration, setupFastifyErrorHandler } from './integrations/tracing/fastify'; diff --git a/packages/node/src/integrations/anr/index.ts b/packages/node/src/integrations/anr/index.ts index 9e979f64e5c3..b93fcfd66612 100644 --- a/packages/node/src/integrations/anr/index.ts +++ b/packages/node/src/integrations/anr/index.ts @@ -1,8 +1,10 @@ +import { types } from 'node:util'; import { Worker } from 'node:worker_threads'; import type { Contexts, Event, EventHint, Integration, IntegrationFn, ScopeData } from '@sentry/core'; import { GLOBAL_OBJ, defineIntegration, + getClient, getCurrentScope, getFilenameToDebugIdMap, getGlobalScope, @@ -14,6 +16,8 @@ import { NODE_VERSION } from '../../nodeVersion'; import type { NodeClient } from '../../sdk/client'; import type { AnrIntegrationOptions, WorkerStartData } from './common'; +const { isPromise } = types; + // This string is a placeholder that gets overwritten with the worker code. export const base64WorkerScript = '###AnrWorkerScript###'; @@ -213,3 +217,26 @@ async function _startWorker( clearInterval(timer); }; } + +export function disableAnrDetectionForCallback(callback: () => T): T; +export function disableAnrDetectionForCallback(callback: () => Promise): Promise; +/** + * Disables ANR detection for the duration of the callback + */ +export function disableAnrDetectionForCallback(callback: () => T | Promise): T | Promise { + const integration = getClient()?.getIntegrationByName(INTEGRATION_NAME) as AnrInternal | undefined; + + if (!integration) { + return callback(); + } + + integration.stopWorker(); + + const result = callback(); + if (isPromise(result)) { + return result.finally(() => integration.startWorker()); + } + + integration.startWorker(); + return result; +} diff --git a/packages/remix/src/index.server.ts b/packages/remix/src/index.server.ts index a92d1d94ca08..f6a5f5060dd9 100644 --- a/packages/remix/src/index.server.ts +++ b/packages/remix/src/index.server.ts @@ -21,6 +21,7 @@ export { addRequestDataToEvent, amqplibIntegration, anrIntegration, + disableAnrDetectionForCallback, captureCheckIn, captureConsoleIntegration, captureEvent, diff --git a/packages/solidstart/src/server/index.ts b/packages/solidstart/src/server/index.ts index 6ff657695081..450420a2b586 100644 --- a/packages/solidstart/src/server/index.ts +++ b/packages/solidstart/src/server/index.ts @@ -13,6 +13,7 @@ export { addRequestDataToEvent, amqplibIntegration, anrIntegration, + disableAnrDetectionForCallback, captureCheckIn, captureConsoleIntegration, captureEvent, diff --git a/packages/sveltekit/src/server/index.ts b/packages/sveltekit/src/server/index.ts index f1c00d2f5e3a..bb88e121244f 100644 --- a/packages/sveltekit/src/server/index.ts +++ b/packages/sveltekit/src/server/index.ts @@ -13,6 +13,7 @@ export { addRequestDataToEvent, amqplibIntegration, anrIntegration, + disableAnrDetectionForCallback, captureCheckIn, captureConsoleIntegration, captureEvent,