From ae05995beb2205b3db25e358c27bc47b8463d436 Mon Sep 17 00:00:00 2001 From: Nev Wylie <54870357+MSNev@users.noreply.github.com> Date: Mon, 28 Apr 2025 15:06:37 -0700 Subject: [PATCH] [main] Handle race condition during unload --- .../src/Sender.ts | 30 ++++++++++++++----- common/config/rush/npm-shrinkwrap.json | 13 -------- tools/chrome-debug-extension/manifest.json | 4 +-- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/channels/applicationinsights-channel-js/src/Sender.ts b/channels/applicationinsights-channel-js/src/Sender.ts index 5a06d257c..4049299a0 100644 --- a/channels/applicationinsights-channel-js/src/Sender.ts +++ b/channels/applicationinsights-channel-js/src/Sender.ts @@ -656,7 +656,12 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls { endpoint: _endpointUrl, version: EnvelopeCreator.Version } as IStatsBeatConfig; - return _self.core.getStatsBeat(statsBeatConfig); + + let core = _self.core; + + // During page unload the core may have been cleared and some async events may not have been sent yet + // resulting in the core being null. In this case we don't want to create a statsbeat instance + return core ? core.getStatsBeat(statsBeatConfig) : null; } function _xdrOnLoad (xdr: IXDomainRequest, payload: IInternalStorageItem[]) { @@ -1304,12 +1309,21 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls { // Using function lookups for backward compatibility as the getNotifyMgr() did not exist until after v2.5.6 function _getNotifyMgr() : INotificationManager { const func = "getNotifyMgr"; - if (_self.core[func]) { - return _self.core[func](); + let result: INotificationManager; + let core = _self.core; + if (core) { + // During page unload the core may have been cleared and some async events may not have been sent yet + // resulting in the core being null. In this case we don't want to create a statsbeat instance + + if (core[func]) { + result = core[func](); + } else { + // using _self.core['_notificationManager'] for backward compatibility + result = (core as any)["_notificationManager"]; + } } - // using _self.core['_notificationManager'] for backward compatibility - return _self.core["_notificationManager"]; + return result; } function _notifySendRequest(sendRequest: SendRequestReason, isAsync: boolean) { @@ -1469,7 +1483,7 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls { /** * error handler - * @Internal + * @internal * since version 3.2.0, if the payload is string[], this function is no-op (string[] is only used for backwards Compatibility) */ public _onError(payload: string[] | IInternalStorageItem[], message: string, event?: ErrorEvent) { @@ -1478,7 +1492,7 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls { /** * partial success handler - * @Internal + * @internal * since version 3.2.0, if the payload is string[], this function is no-op (string[] is only used for backwards Compatibility) */ public _onPartialSuccess(payload: string[] | IInternalStorageItem[], results: IBackendResponse) { @@ -1487,7 +1501,7 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControls { /** * success handler - * @Internal + * @internal * since version 3.2.0, if the payload is string[], this function is no-op (string[] is only used for backwards Compatibility) */ public _onSuccess(payload: string[] | IInternalStorageItem[], countOfItemsInPayload: number) { diff --git a/common/config/rush/npm-shrinkwrap.json b/common/config/rush/npm-shrinkwrap.json index b843d7618..7f14d9349 100644 --- a/common/config/rush/npm-shrinkwrap.json +++ b/common/config/rush/npm-shrinkwrap.json @@ -3540,19 +3540,6 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" }, - "node_modules/fsevents": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", - "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/tools/chrome-debug-extension/manifest.json b/tools/chrome-debug-extension/manifest.json index 9b234f5a8..9de91a8a1 100644 --- a/tools/chrome-debug-extension/manifest.json +++ b/tools/chrome-debug-extension/manifest.json @@ -2,8 +2,8 @@ "name": "Telemetry Viewer - M3", "short_name": "Telemetry Viewer M3", "description": "A browser extension that provides a real time view of what's happening in Application Insights including what telemetry is being logged by the web application", - "version": "0.7.5", - "version_name": "0.7.5", + "version": "0.8.0", + "version_name": "0.8.0", "manifest_version": 3, "icons": { "16": "images/icon-16.png",