Skip to content

🐛 [RUM Profiler] Fix stateReason not updated on stop and cleanup tasks accumulation#4171

Open
thomasbertet wants to merge 3 commits intomainfrom
thomas.bertet/PROF-13743-fix-profiler-stop-state-and-cleanup
Open

🐛 [RUM Profiler] Fix stateReason not updated on stop and cleanup tasks accumulation#4171
thomasbertet wants to merge 3 commits intomainfrom
thomas.bertet/PROF-13743-fix-profiler-stop-state-and-cleanup

Conversation

@thomasbertet
Copy link
Collaborator

Motivation

Two issues found in the profiler state machine after the recent sync state fix (#4152):

  1. stopProfilerInstance does not update stateReason when the profiler is already in stopped state. If SESSION_EXPIRED fires first (setting stateReason to session-expired), then the user calls stop(), the reason stays session-expired. On the next SESSION_RENEWED, the profiler restarts against the user's intent.

  2. globalCleanupTasks array is never cleared after forEach cleanup in stopProfiling. Stale function references accumulate across start/stop cycles.

Changes

  • stopProfilerInstance: when already stopped, update stateReason if the new reason is stopped-by-user, so that SESSION_RENEWED respects the user's explicit stop.
  • stopProfiling: reset globalCleanupTasks.length = 0 after invoking all cleanup functions.

Test instructions

  • Run yarn test:unit --spec packages/rum/src/domain/profiling/profiler.spec.ts
  • New test: "should not restart profiling on session renewal if user called stop after session expiration"

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

stopProfilerInstance did not update stateReason when the profiler was
already in stopped state. This meant calling stop() after SESSION_EXPIRED
left stateReason as 'session-expired', causing SESSION_RENEWED to restart
the profiler against the user's intent.
The array was never reset after forEach, causing stale cleanup function
references to accumulate across start/stop cycles.
@thomasbertet thomasbertet marked this pull request as ready for review February 12, 2026 17:56
@thomasbertet thomasbertet requested a review from a team as a code owner February 12, 2026 17:56
@thomasbertet
Copy link
Collaborator Author

/to-staging

@gh-worker-devflow-routing-ef8351
Copy link

gh-worker-devflow-routing-ef8351 bot commented Feb 18, 2026

View all feedbacks in Devflow UI.

2026-02-18 17:39:35 UTC ℹ️ Start processing command /to-staging


2026-02-18 17:39:42 UTC ℹ️ Branch Integration: starting soon, merge expected in approximately 24m (p90)

Commit d1b7c242b7 will soon be integrated into staging-08.


2026-02-18 17:53:06 UTC ℹ️ Branch Integration: this commit was successfully integrated

Commit d1b7c242b7 has been merged into staging-08 in merge commit a92a03b2bf.

Check out the triggered DDCI request.

If you need to revert this integration, you can use the following command: /code revert-integration -b staging-08

gh-worker-dd-mergequeue-cf854d bot added a commit that referenced this pull request Feb 18, 2026
#4171) into staging-08

Integrated commit sha: d1b7c24

Co-authored-by: thomasbertet <thomas.bertet@datadoghq.com>
instance = { state: 'stopped', stateReason }
}
return
}
Copy link
Member

Choose a reason for hiding this comment

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

nitpick (non-blocking): This could be slightly refactored I think:

if (instance.state !== "running") {
  if (
    // If paused, profiler data was already collected during pause, just update state
    instance.state === "paused" ||
    // Update stateReason when already stopped and the user explicitly stops the profiler,
    // so that SESSION_RENEWED does not override the user's intent.
    (instance.state === "stopped" && stateReason === "stopped-by-user")
  ) {
    instance = { state: "stopped", stateReason };
  }

  return;
}

Copy link
Collaborator Author

@thomasbertet thomasbertet Feb 19, 2026

Choose a reason for hiding this comment

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

Done in 9b7dcb8 — merged both branches into a single if (instance.state !== 'running') guard.

Comment on lines 658 to 660
// Wait a bit to ensure profiler doesn't restart
await new Promise((resolve) => setTimeout(resolve, 100))

Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Let's not wait for arbitrary amount of times in tests. Is it useful to wait at all since start is synchronous?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call — removed the arbitrary wait since start() is synchronous. Done in 9b7dcb8.

Refactor stopProfilerInstance to merge paused and stopped early-return
branches into a single guard. Remove arbitrary setTimeout wait in test
since start() is synchronous.
@cit-pr-commenter-54b7da
Copy link

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 169.43 KiB 169.43 KiB 0 B 0.00%
Rum Profiler 4.29 KiB 4.34 KiB +50 B +1.14%
Rum Recorder 24.54 KiB 24.54 KiB 0 B 0.00%
Logs 56.72 KiB 56.72 KiB 0 B 0.00%
Flagging 944 B 944 B 0 B 0.00%
Rum Slim 126.26 KiB 126.26 KiB 0 B 0.00%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance

Pending...

🧠 Memory Performance

Pending...

🔗 RealWorld

@datadog-datadog-prod-us1
Copy link

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

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

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

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 9b7dcb8 | Docs | Datadog PR Page | Was this helpful? Give us feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments