Skip to content

Commit

Permalink
feat(node): Add disableAnrDetectionForCallback function (#14359)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
timfish authored Dec 4, 2024
1 parent 1ec4c49 commit 20c267e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {
extractRequestData,
createGetModuleFromFilename,
anrIntegration,
disableAnrDetectionForCallback,
consoleIntegration,
httpIntegration,
nativeNodeFetchIntegration,
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions packages/node/src/integrations/anr/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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###';

Expand Down Expand Up @@ -213,3 +217,26 @@ async function _startWorker(
clearInterval(timer);
};
}

export function disableAnrDetectionForCallback<T>(callback: () => T): T;
export function disableAnrDetectionForCallback<T>(callback: () => Promise<T>): Promise<T>;
/**
* Disables ANR detection for the duration of the callback
*/
export function disableAnrDetectionForCallback<T>(callback: () => T | Promise<T>): T | Promise<T> {
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;
}
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/solidstart/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
addRequestDataToEvent,
amqplibIntegration,
anrIntegration,
disableAnrDetectionForCallback,
captureCheckIn,
captureConsoleIntegration,
captureEvent,
Expand Down

0 comments on commit 20c267e

Please sign in to comment.