From 453f5052569dafb52e82e875a8976cf348ed16d4 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Tue, 14 Jan 2025 21:23:42 +0100 Subject: [PATCH] [DevTools] Prevent crash when starting consecutive profiling sessions (#32066) ## Summary This pull request resolves an issue where consecutive profiling sessions would cause Dev Tools to freeze due to an infinite loop of state updates. The problem occurs when the startProfiling function triggers a call to [`selectCommitIndex(0)` in SnapshotSelector](https://github.com/facebook/react/blob/b3a95caf61bc716fb618997e6e9f3a0c8c9c8374/packages/react-devtools-shared/src/devtools/views/Profiler/SnapshotSelector.js#L77-L85) as previous profiling data is available, which causes a re-render. Then, [ProfilerContextProvider calls `selectCommitIndex(null)`](https://github.com/facebook/react/blob/b3a95caf61bc716fb618997e6e9f3a0c8c9c8374/packages/react-devtools-shared/src/devtools/views/Profiler/ProfilerContext.js#L231-L241) to clear the view while profiling is in progress, leading to another re-render and creating an infinite loop. This behavior was prevented by clearing the existing profiling data before starting a new session. Closes #31977 Closes #31679 ## How did you test this change? I ran the Dev Tools locally following [the contributing guideline](https://github.com/facebook/react/blob/b3a95caf61bc716fb618997e6e9f3a0c8c9c8374/packages/react-devtools/CONTRIBUTING.md). I observed the freeze at the start of the second profiling session. Then, I modified the code to clear the store when starting a new session and ran the Dev Tools again. This time, no freeze was observed. Before: https://github.com/user-attachments/assets/9d790f84-f6d0-4951-8202-e599cf8d225b After: https://github.com/user-attachments/assets/af097019-0b8f-49dd-8afc-0f6cd72af787 --- packages/react-devtools-shared/src/devtools/ProfilerStore.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/react-devtools-shared/src/devtools/ProfilerStore.js b/packages/react-devtools-shared/src/devtools/ProfilerStore.js index b9bbdb11df..33240bd77f 100644 --- a/packages/react-devtools-shared/src/devtools/ProfilerStore.js +++ b/packages/react-devtools-shared/src/devtools/ProfilerStore.js @@ -191,6 +191,8 @@ export default class ProfilerStore extends EventEmitter<{ } startProfiling(): void { + this.clear(); + this._bridge.send('startProfiling', { recordChangeDescriptions: this._store.recordChangeDescriptions, recordTimeline: this._store.supportsTimeline,