diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 68c31a797e..4f553c1bd9 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -15,6 +15,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2 ### :bug: Bug Fixes * fix(sdk-node): fix setting of ViewOption#name from ConfigurationModel [#6620](https://github.com/open-telemetry/opentelemetry-js/pull/6620) @trentm +* fix(web-common): add limit for timeout [#6601](https://github.com/open-telemetry/opentelemetry-js/pull/6601) @maryliag ### :books: Documentation diff --git a/experimental/packages/web-common/src/SessionManager.ts b/experimental/packages/web-common/src/SessionManager.ts index 16c84f8dd2..3b5835b8c1 100644 --- a/experimental/packages/web-common/src/SessionManager.ts +++ b/experimental/packages/web-common/src/SessionManager.ts @@ -10,6 +10,7 @@ import type { SessionObserver } from './types/SessionObserver'; import type { SessionStore } from './types/SessionStore'; import type { SessionPublisher } from './types/SessionPublisher'; +const MAX_DURATION_MS = 2147483647; // max setTimeout value in milliseconds (~24.8 days) export interface SessionManagerConfig { /** Class responsible for generating a session ID */ sessionIdGenerator: SessionIdGenerator; @@ -145,9 +146,10 @@ export class SessionManager implements SessionProvider, SessionPublisher { clearTimeout(this._inactivityTimeoutId); } + const timeoutIn = Math.min(this._inactivityTimeout * 1000, MAX_DURATION_MS); this._inactivityTimeoutId = setTimeout(() => { this.resetSession(); - }, this._inactivityTimeout * 1000); + }, timeoutIn); } private resetMaxDurationTimer() { @@ -159,8 +161,13 @@ export class SessionManager implements SessionProvider, SessionPublisher { clearTimeout(this._maxDurationTimeoutId); } - const timeoutIn = - this._maxDuration * 1000 - (Date.now() - this._session?.startTimestamp); + const timeoutIn = Math.max( + 0, // a backgrounded tab can cause a negative timeout value, so we use 0 to trigger session reset immediately on resume of the tab + Math.min( + this._maxDuration * 1000 - (Date.now() - this._session.startTimestamp), + MAX_DURATION_MS + ) + ); this._maxDurationTimeoutId = setTimeout(() => { this.resetSession();