Repository navigation
refactor(rum): Simplify profiling state and lifecycle handling - #5364
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Bundles Sizes Evolution
|
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: e1b11dd | Docs | View more details | Give us feedback! |
There was a problem hiding this comment.
More details
The lifecycle changes yielded no reportable defects in static review. Runtime verification remains incomplete because the required Playwright Chromium executable is missing.
🤖 Bits Code Review · Commit 43a4438
thomasbertet
left a comment
There was a problem hiding this comment.
Nice simplification overall — unsubscribing the lifeCycle listeners in stop() and avoiding duplicate window listeners on start() are good improvements, and the normal lifecycle (visibility pause/resume, session expiry/renewal, quota checks, and cleanup) behaves the same as before.
A few findings inline, most notably a behavioral change on startup failures followed by SESSION_RENEWED.
| } as any) | ||
|
|
||
| setVisibilityState('visible') | ||
| window.dispatchEvent(createNewEvent('beforeunload')) |
There was a problem hiding this comment.
not sure why you added these new dispatched event ? Seems unrelated to the test 🤔
There was a problem hiding this comment.
Removed the extra beforeunload dispatches from both existing tests in e1b11dd and restored the visibility test's original name. The paused-profiler unload behavior is covered by its dedicated regression test. All 46 profiling unit tests pass.
| addEventListener(window, DOM_EVENT.BEFORE_UNLOAD, () => { | ||
| if (instance.state !== 'stopped') { | ||
| startNextProfilerInstance() | ||
| } | ||
| }).stop, |
There was a problem hiding this comment.
Minor (pre-existing behavior): instance.state !== 'stopped' also matches 'paused'. When paused, pauseProfilerInstance() has already stopped and assembled the running profiler, so there is nothing left to flush — yet beforeunload will spin up a brand-new Profiler instance on a hidden tab that is unloading.
Changing this guard to if (instance.state === 'running') would avoid creating a new profiler when already paused (unless starting one on beforeunload while hidden is intentional).
There was a problem hiding this comment.
Fixed in 5961cae: the beforeunload guard now checks instance.state === 'running'. Added a regression test verifying that unloading while paused keeps the profiler paused, creates no new instance, and sends no additional profile. All 46 profiling unit tests pass.
| lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, () => { | ||
| if (instance.state === 'stopped' && isCurrentSessionSampled()) { | ||
| startProfiling() | ||
| } | ||
| }).unsubscribe |
There was a problem hiding this comment.
Behavioral change on startup failure: because cleanupTasks are registered before startProfiling(), if startNextProfilerInstance() fails (missing-document-policy-header, unexpected-exception) or throws (not-supported-by-browser), instance remains { state: 'stopped' } while SESSION_RENEWED stays subscribed.
Previously, stateReason remained 'initializing' (even after SESSION_EXPIRED), so SESSION_RENEWED never retried. Now every sampled SESSION_RENEWED calls startProfiling() again:
missing-document-policy-header: retriesnew Profiler(), logs the warning again, and fires another/api/v2/profiling/quotarequest even thoughDocument-Policycannot change for the document.unexpected-exception: retriesnew Profiler()and sends another quota request on each renewal.not-supported-by-browser: throws an uncaughtErrorinside theSESSION_RENEWEDcallback (for direct callers ofcreateRumProfiler).
Consider only registering cleanupTasks once startup succeeds (or cleaning them up on startup failure), and adding unit tests covering startup failure + SESSION_RENEWED.
There was a problem hiding this comment.
Fixed in 9285451: start() now registers the listeners only after startup succeeds, so an initial failure leaves no SESSION_RENEWED subscription to retry startup. Added a regression test covering constructor failure followed by session expiry and renewal, checking that neither the constructor nor the quota check runs again.
| stopRunningProfiler(previousInstance, { discard: reason === 'quota_ko' }) | ||
| } | ||
| if (previousInstance.state === 'running') { | ||
| stopRunningProfiler(previousInstance, { assemble: quotaReason === undefined }) |
There was a problem hiding this comment.
Deriving assemble from quotaReason === undefined couples the discard decision to telemetry metadata. With the current checkProfilingQuota, result.reason is always a string, so runtime behavior matches today — but if quota_ko ever arrives with reason: undefined (e.g. a mock or future change), assemble silently flips to true and sends the profile despite quota denial.
Passing an explicit option (e.g. stopProfiling({ assemble = true, quotaReason }: { assemble?: boolean; quotaReason?: QuotaReason } = {})) would keep the intent explicit.
There was a problem hiding this comment.
Updated in 2c00f5c: stopProfiling now receives the full QuotaResult and uses quotaResult?.decision !== 'quota_ko' to control assembly. The reason is only used for profiling context metadata, so a missing reason cannot cause a denied profile to be sent. All 46 profiling unit tests pass.
Reconcile the Web Worker Profiling extension seam with the profiling state and lifecycle refactor from #5364: - keep main's simplified start/stop lifecycle (cleanupTasks, QuotaResult, no stateReason) - re-add the ProfilerExtension/ProfilerHost seam: mainThreadId shared with profileAssembly, attachExtension, onStart/onStop/onFlush hooks, and the stoppedByUser distinction for closePorts - stopProfiling() semantics: quota_ko discards extensions, stopped-by-user closes their ports, session expiry keeps them open for renewal
Motivation
Simplify profiling state and lifecycle handling
Changes
discardwith the positiveassembleoption and extend lifecycle regression coverage.Test instructions
Run the targeted unit tests:
Expected: 57 tests pass. Coverage includes visibility pause/resume, session expiry and renewal, quota denial, stale quota responses, and controller cleanup followed by an explicit restart.
Validation completed locally: 57 Chromium unit tests, source type-checking (
tsconfig.default.json), lint on changed files, and formatting. The local dependency setup required invoking Vitest throughyarn nodewith a temporary cache configuration. Full-repository type-checking is blocked by missingrelease-pleasedependencies in the release scripts. Manual/staging testing was not performed.Checklist