Skip to content

♻️ Rewrite session storage with CookieAccessor and native Web Locks#4252

Draft
thomas-lebeau wants to merge 5 commits intothomas.lebeau/v7-simplify-session-managersfrom
thomas.lebeau/v7-cookieStore-nativeLock
Draft

♻️ Rewrite session storage with CookieAccessor and native Web Locks#4252
thomas-lebeau wants to merge 5 commits intothomas.lebeau/v7-simplify-session-managersfrom
thomas.lebeau/v7-cookieStore-nativeLock

Conversation

@thomas-lebeau
Copy link
Collaborator

Motivation

The session management layer had accumulated complexity: custom cookie locking logic, duplicated session managers across packages (rum, logs), tightly coupled session store strategies, and synchronous cookie access patterns. This PR rewrites the session storage stack to be simpler, more robust, and aligned with v7 goals.

Changes

  • Replace the custom cookie lock mechanism with the native Web Locks API for cross-tab coordination
  • Introduce a CookieAccessor abstraction layer that decouples session logic from raw cookie I/O
  • Make SessionStoreStrategy fully async with event-driven change detection instead of polling
  • Move cookieObservable from rum-core to core for proper shared ownership
  • Consolidate rumSessionManager and logsSessionManager into a single core SessionManager
  • Merge TrackedSession into SessionContext, extract SessionReplayState and computeSessionReplayState
  • Handle bridge environments in computeSessionReplayState
  • Extract recording condition helpers in postStartStrategy
  • Make all sampling decisions deterministic
  • Consolidate session manager test mocks and specs into core and rum-core

Test instructions

  • yarn test:unit — all unit tests should pass
  • yarn test:e2e — session store and tracking consent scenarios updated

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

- Remove sessionStoreOperations and its cookie-based lock mechanism
- Introduce sessionLock module using navigator.locks for concurrency
- Simplify sessionStore by inlining retrieve/persist logic within lock
- Remove isLockEnabled from SessionStoreStrategy interface
- Clean up related tests and fake session store strategy
The cookieObservable only depends on browser-core types and will be
needed by the session cookie strategy in core. Move it to core and
re-export from rum-core to keep existing imports working.
Introduce a CookieAccessor interface that abstracts cookie operations.
Uses the CookieStore API when available for async native cookie access,
and falls back to document.cookie with Promise wrappers.

Also add set() and delete() to the CookieStore type definition.
Make the SessionStoreStrategy interface async (methods return Promises)
to support the CookieStore API which is inherently asynchronous.

Key changes:
- SessionStoreStrategy methods return Promises
- sessionLock supports async callbacks with promise-chain fallback
  when navigator.locks is unavailable
- sessionStore lock callbacks are async, expire() runs inside lock
- Replace polling-based session watching with onExternalChange:
  cookie strategy uses CookieStore events or interval polling,
  localStorage strategy uses native storage events
- Cookie strategy uses CookieAccessor for read/write operations
- All test suites updated with flushLock() async drain pattern
@github-actions
Copy link

github-actions bot commented Feb 27, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@cit-pr-commenter-54b7da
Copy link

cit-pr-commenter-54b7da bot commented Feb 27, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 171.87 KiB 172.20 KiB +332 B +0.19%
Rum Profiler 4.67 KiB 4.67 KiB 0 B 0.00%
Rum Recorder 24.88 KiB 25.10 KiB +227 B +0.89%
Logs 56.61 KiB 57.41 KiB +825 B +1.42%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 127.50 KiB 127.94 KiB +460 B +0.35%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0055 0.0041 -25.45%
RUM - add action 0.0198 0.0174 -12.12%
RUM - add error 0.019 0.0161 -15.26%
RUM - add timing 0.0051 0.003 -41.18%
RUM - start view 0.0221 0.0161 -27.15%
RUM - start/stop session replay recording 0.0013 0.0008 -38.46%
Logs - log message 0.0268 0.0184 -31.34%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 27.38 KiB 26.34 KiB -1.04 KiB
RUM - add action 115.40 KiB 303.05 KiB +187.66 KiB
RUM - add timing 26.00 KiB 47.83 KiB +21.83 KiB
RUM - add error 119.37 KiB 296.13 KiB +176.76 KiB
RUM - start/stop session replay recording 26.10 KiB 32.10 KiB +6.00 KiB
RUM - start view 503.45 KiB 644.94 KiB +141.50 KiB
Logs - log message 46.31 KiB 230.33 KiB +184.02 KiB

🔗 RealWorld

@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Feb 27, 2026

⚠️ Tests

Fix all issues with BitsAI or with Cursor

⚠️ Warnings

🧪 3147 Tests failed

AbstractLifeCycle does nothing when notifying without subscribers from Chrome 63.0.3239.84 (Windows 10) (Datadog) (Fix with Cursor)
ReferenceError: globalThis is not defined
    at UserContext.afterEach (webpack:///packages/core/src/domain/session/storeStrategies/sessionInCookie.spec.ts:25:5 <- /tmp/_karma_webpack_358081/commons.js:45227:9)
    at <Jasmine>
AbstractLifeCycle does not notify unsubscribed subscribers from Chrome 63.0.3239.84 (Windows 10) (Datadog) (Fix with Cursor)
ReferenceError: globalThis is not defined
    at UserContext.afterEach (webpack:///packages/core/src/domain/session/storeStrategies/sessionInCookie.spec.ts:25:5 <- /tmp/_karma_webpack_358081/commons.js:45227:9)
    at <Jasmine>
AbstractLifeCycle notifies subscribers from Chrome 63.0.3239.84 (Windows 10) (Datadog) (Fix with Cursor)
ReferenceError: globalThis is not defined
    at UserContext.afterEach (webpack:///packages/core/src/domain/session/storeStrategies/sessionInCookie.spec.ts:25:5 <- /tmp/_karma_webpack_358081/commons.js:45227:9)
    at <Jasmine>
View all

ℹ️ Info

❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 66.42%
Overall Coverage: 76.69% (-0.21%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 7fcd324 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@thomas-lebeau thomas-lebeau changed the base branch from main to thomas.lebeau/v7-simplify-session-managers February 27, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant