Skip to content

fix(node): Only require inspector when needed #9149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/node/src/anr/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Event, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';
import { spawn } from 'child_process';
import * as inspector from 'inspector';

import { addGlobalEventProcessor, captureEvent, flush } from '..';
import { captureStackTrace } from './debugger';
Expand Down Expand Up @@ -98,12 +97,19 @@ function sendEvent(blockedMs: number, frames?: StackFrame[]): void {
});
}

interface InspectorApi {
open: (port: number) => void;
url: () => string | undefined;
}

/**
* Starts the node debugger and returns the inspector url.
*
* When inspector.url() returns undefined, it means the port is already in use so we try the next port.
*/
function startInspector(startPort: number = 9229): string | undefined {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const inspector: InspectorApi = require('inspector');
let inspectorUrl: string | undefined = undefined;
let port = startPort;

Expand Down