Event Loop Block Detection

Monitor for blocked event loops in Node.js applications

Event Loop Block detection monitors when the Node.js main thread event loop is blocked for more than a specified threshold. The Node SDK reports these events to Sentry with automatically captured stack traces to help identify blocking code.

For the best performance and comprehensive monitoring, we recommend using the eventLoopBlockIntegration from the @sentry/node-native package. Stack traces are automatically captured when blocking is detected. This integration can monitor all threads in your Node.js application and provides better performance compared to the deprecated ANR integration.

Copied
import * as Sentry from "@sentry/node";
import { eventLoopBlockIntegration } from "@sentry/node-native";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [eventLoopBlockIntegration({ threshold: 1000 })],
});

For detailed usage instructions and configuration options, see the eventLoopBlockIntegration documentation.

(Available in version 7.91.0 and above)

The legacy ANR integration is still available but deprecated. If you're currently using it, we recommend migrating to the new eventLoopBlockIntegration.

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [Sentry.anrIntegration({ captureStackTrace: true })],
});

Example of an ANR error event

The deprecated ANR integration supports the following configuration options:

Copied
interface Options {
  /**
   * Interval to send heartbeat messages to the ANR thread.
   *
   * Defaults to 50ms.
   */
  pollInterval: number;
  /**
   * Threshold in milliseconds to trigger an ANR event.
   *
   * Defaults to 5000ms.
   */
  anrThreshold: number;
  /**
   * Whether to capture a stack trace when the ANR event is triggered.
   *
   * Defaults to `false`.
   *
   * This uses the node debugger which enables the inspector API.
   */
  captureStackTrace: boolean;
}

ANR detection with the legacy Node SDK uses a worker thread to monitor the event loop in the main app thread. The main app thread sends a heartbeat message to the ANR worker thread every 50ms. If the ANR worker does not receive a heartbeat message for 5 seconds, it triggers an ANR event. If the captureStackTrace option is enabled, the ANR worker uses the inspector module to capture stack traces via the v8 inspector API.

Once an ANR event is reported, the ANR worker thread exits to prevent further duplicate events and the main app thread will continue to run as usual.

Overhead from Node.js ANR tracking should be minimal. With no ANR detected, the only overhead in the main app thread is polling the ANR worker over IPC every 50ms by default. The ANR worker thread consumes around 10-20 MB of RAM to keep track of the polling. Once an ANR has been detected, the main thread is paused briefly in the debugger to capture the stack trace frames. At this point, the event loop has been blocked for seconds so the debugging overhead is negligible.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").