Skip to content

🐛 Align session-ended WebSocket teardown with the latest view's end clocks - #4911

Merged
bdibon merged 2 commits into
mainfrom
boris.dibon/fix-websocket-closed-vital
Jul 31, 2026
Merged

🐛 Align session-ended WebSocket teardown with the latest view's end clocks#4911
bdibon merged 2 commits into
mainfrom
boris.dibon/fix-websocket-closed-vital

Conversation

@bdibon

@bdibon bdibon commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Motivation

When a RUM session expires, open WebSocket connections are finalized with tracking_end_reason: 'session_end'. The teardown used clocksNow() independently in both trackViews (to end the active view) and webSocketCollection (to close the websocket vital/resource). Because these two calls happened at slightly different times, the websocket's end_time could land after the view's end_time — causing the websocket-closed vital to be dropped during assembly and the resource's end_time to fall outside the lifespan of the view it was attached to.

Changes

  • Add a SessionExpiredEvent type ({ endClocks: ClocksState }) carried on the SESSION_EXPIRED lifecycle event, instead of void.
  • startRum now captures clocksNow() once when the session expires and notifies SESSION_EXPIRED with those clocks, so every subscriber ends things at the exact same instant.
  • trackViews ends the current view using the shared endClocks from SESSION_EXPIRED instead of computing its own.
  • webSocketCollection.flushOpenConnections now accepts an optional endClocks param, using the shared clocks passed on SESSION_EXPIRED (falling back to clocksNow() for its own stop() call, where no shared clocks exist).
  • Unit tests covering the new SessionExpiredEvent payload and the wired session-end path in startRum, trackViews, and webSocketCollection.
  • E2E test (renamed to "collects the websocket-closed vital when the session expires") asserting the websocket-closed vital and resource end_time do not exceed the associated view's end time.

Test instructions

yarn test:unit --spec packages/browser-rum-core/src/boot/startRum.spec.ts
yarn test:unit --spec packages/browser-rum-core/src/domain/view/trackViews.spec.ts
yarn test:unit --spec packages/browser-rum-core/src/domain/resource/webSocketCollection.spec.ts
yarn build:apps && yarn test:e2e -g "collects the websocket-closed vital when the session expires"

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 24, 2026

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 180.54 KiB 180.59 KiB +57 B +0.03%
Rum Profiler 8.43 KiB 8.43 KiB 0 B 0.00%
Rum Recorder 21.12 KiB 21.12 KiB 0 B 0.00%
Logs 57.05 KiB 57.05 KiB 0 B 0.00%
Rum Salesforce N/A 138.47 KiB N/A N/A N/A
Rum Slim 138.41 KiB 138.46 KiB +50 B +0.04%
Worker 22.96 KiB 22.96 KiB 0 B 0.00%
Rum Shopify N/A 200.17 KiB N/A N/A N/A
Rum-shopify Profiler N/A 8.43 KiB N/A N/A N/A
Rum-shopify Recorder N/A 3.72 KiB N/A N/A N/A

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 24, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 77.14% (+0.00%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 79de282 | Docs | Datadog PR Page | Give us feedback!

@bdibon
bdibon marked this pull request as ready for review July 27, 2026 09:54
@bdibon
bdibon requested a review from a team as a code owner July 27, 2026 09:54
@sbarrio
sbarrio requested a review from rgaignault July 28, 2026 07:05
@bdibon
bdibon force-pushed the boris.dibon/fix-websocket-closed-vital branch from 700d262 to 79de282 Compare July 30, 2026 15:55

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79de282d72

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-core/src/domain/lifeCycle.ts
Comment on lines +101 to +106
const associatedView = intakeRegistry.rumViewEvents.find((event) => event.view.id === closedVital!.view.id)
expect(associatedView).toBeDefined()

const viewEndTime = associatedView!.date + associatedView!.view.time_spent / NANOSECONDS_PER_MILLISECOND
expect(closedVital!.date).toBeLessThanOrEqual(viewEndTime)
expect(wsWithSessionEnd!.resource.websocket.end_time).toBeLessThanOrEqual(viewEndTime)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick: ‏Do we need to check that? I guess ensuring that we are collecting the closing vital should be enough for an e2e scenario

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't cost us much to have this and it prevents any regression where the vital is emitted with an incoherent timestamp.

@bdibon
bdibon merged commit 7cd462a into main Jul 31, 2026
32 checks passed
@bdibon
bdibon deleted the boris.dibon/fix-websocket-closed-vital branch July 31, 2026 08:54
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants