Skip to content
Merged
Show file tree
Hide file tree
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
65 changes: 44 additions & 21 deletions packages/browser/src/BacktraceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { BacktraceConfiguration } from './BacktraceConfiguration';
import { BacktraceClientBuilder } from './builder/BacktraceClientBuilder';

export class BacktraceClient extends BacktraceCoreClient {
private readonly _disposeController: AbortController = new AbortController();

protected static _instance?: BacktraceClient;
constructor(
options: BacktraceConfiguration,
Expand Down Expand Up @@ -72,32 +74,53 @@ export class BacktraceClient extends BacktraceCoreClient {
return this._instance;
}

/**
* Disposes the client and all client callbacks
*/
public dispose(): void {
this._disposeController.abort();
super.dispose();
BacktraceClient._instance = undefined;
}

private captureUnhandledErrors(captureUnhandledExceptions = true, captureUnhandledRejections = true) {
if (captureUnhandledExceptions) {
window.addEventListener('error', async (errorEvent: ErrorEvent) => {
await this.send(
new BacktraceReport(errorEvent.error, {
'error.type': 'Unhandled exception',
}),
);
});
window.addEventListener(
'error',
async (errorEvent: ErrorEvent) => {
await this.send(
new BacktraceReport(errorEvent.error, {
'error.type': 'Unhandled exception',
}),
);
},
{
signal: this._disposeController.signal,
},
);
}

if (captureUnhandledRejections) {
window.addEventListener('unhandledrejection', async (errorEvent: PromiseRejectionEvent) => {
await this.send(
new BacktraceReport(
errorEvent.reason,
{
'error.type': 'Unhandled exception',
},
[],
{
classifiers: ['UnhandledPromiseRejection'],
},
),
);
});
window.addEventListener(
'unhandledrejection',
async (errorEvent: PromiseRejectionEvent) => {
await this.send(
new BacktraceReport(
errorEvent.reason,
{
'error.type': 'Unhandled exception',
},
[],
{
classifiers: ['UnhandledPromiseRejection'],
},
),
);
},
{
signal: this._disposeController.signal,
},
);
}
}
}
17 changes: 16 additions & 1 deletion packages/node/src/BacktraceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { NodeDiagnosticReportConverter } from './converter/NodeDiagnosticReportC
import { BacktraceDatabaseFileStorageProvider } from './database/BacktraceDatabaseFileStorageProvider';

export class BacktraceClient extends BacktraceCoreClient {
private _listeners: Record<string, NodeJS.UnhandledRejectionListener | NodeJS.UncaughtExceptionListener> = {};

private static _instance?: BacktraceClient;
constructor(
options: CoreConfiguration,
Expand Down Expand Up @@ -77,6 +79,18 @@ export class BacktraceClient extends BacktraceCoreClient {
return this._instance;
}

/**
* Disposes the client and all client callbacks
*/
public dispose(): void {
for (const [name, listener] of Object.entries(this._listeners)) {
process.removeListener(name, listener);
}

super.dispose();
BacktraceClient._instance = undefined;
}

private captureUnhandledErrors(captureUnhandledExceptions = true, captureUnhandledRejections = true) {
if (!captureUnhandledExceptions && !captureUnhandledRejections) {
return;
Expand All @@ -97,7 +111,7 @@ export class BacktraceClient extends BacktraceCoreClient {
};

process.prependListener('uncaughtExceptionMonitor', captureUncaughtException);

this._listeners['uncaughtExceptionMonitor'] = captureUncaughtException;
if (!captureUnhandledRejections) {
return;
}
Expand Down Expand Up @@ -180,6 +194,7 @@ export class BacktraceClient extends BacktraceCoreClient {
process.emitWarning(warning);
};
process.prependListener('unhandledRejection', captureUnhandledRejectionsCallback);
this._listeners['unhandledRejection'] = captureUnhandledRejectionsCallback;
}

private captureNodeCrashes() {
Expand Down
23 changes: 23 additions & 0 deletions packages/node/tests/client/disposeTests.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BacktraceClient } from '../../src/';
describe('Client Dispose tests', () => {
it('Should dispose process callbacks', () => {
const expectedUnhandledRejectionListenerCount = process.listenerCount('unhandledRejection');
const expectedUncaughtExceptionListenerCount = process.listenerCount('uncaughtExceptionMonitor');
const client = BacktraceClient.initialize({
url: 'https://submit.backtrace.io/foo/bar/baz',
metrics: {
enable: false,
},
breadcrumbs: {
enable: false,
},
});

expect(
process.listenerCount('unhandledRejection') + process.listenerCount('uncaughtExceptionMonitor'),
).toBeGreaterThan(expectedUnhandledRejectionListenerCount + expectedUncaughtExceptionListenerCount);
client.dispose();
expect(process.listenerCount('unhandledRejection')).toBe(expectedUnhandledRejectionListenerCount);
expect(process.listenerCount('uncaughtExceptionMonitor')).toBe(expectedUncaughtExceptionListenerCount);
});
});
23 changes: 23 additions & 0 deletions packages/sdk-core/src/BacktraceCoreClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import { MetricsBuilder } from './modules/metrics/MetricsBuilder';
import { SingleSessionProvider } from './modules/metrics/SingleSessionProvider';
import { RateLimitWatcher } from './modules/rateLimiter/RateLimitWatcher';
export abstract class BacktraceCoreClient {
/**
* Determines if the client is enabled.
*/
public get enabled() {
return this._enabled;
}

/**
* Current session id
*/
Expand Down Expand Up @@ -87,6 +94,8 @@ export abstract class BacktraceCoreClient {
private readonly _sdkOptions: SdkOptions;
protected readonly options: BacktraceConfiguration;

private _enabled = false;

protected constructor(private readonly _setup: CoreClientSetup) {
this.options = _setup.options;
this._sdkOptions = _setup.sdkOptions;
Expand Down Expand Up @@ -140,6 +149,7 @@ export abstract class BacktraceCoreClient {
}

this.initialize();
this._enabled = true;
}

/**
Expand Down Expand Up @@ -182,6 +192,9 @@ export abstract class BacktraceCoreClient {
reportAttributes: Record<string, unknown> = {},
reportAttachments: BacktraceAttachment[] = [],
): Promise<void> {
if (!this._enabled) {
return;
}
if (this._rateLimitWatcher.skipReport()) {
return;
}
Expand Down Expand Up @@ -215,6 +228,16 @@ export abstract class BacktraceCoreClient {
}
}

/**
* Disposes the client and all client callbacks
*/
public dispose() {
this._enabled = false;
this.database?.dispose();
this.breadcrumbsManager?.dispose();
this._metrics?.dispose();
}

private addToDatabase(
data: BacktraceData,
attachments: BacktraceAttachment[],
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-core/src/modules/metrics/BacktraceMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class BacktraceMetrics {
/**
* Cleans up metrics interface.
*/
public close() {
public dispose() {
if (this._updateIntervalId) {
clearInterval(this._updateIntervalId);
}
Expand Down