Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(monaco): guard editor onMount against unmounted component (#71024)

posthog[bot]16af30b84844
diff --git a/frontend/src/lib/monaco/CodeEditorImpl.tsx b/frontend/src/lib/monaco/CodeEditorImpl.tsx
index 31d7ae9b3f94..146ee8bcd108 100644
--- a/frontend/src/lib/monaco/CodeEditorImpl.tsx
+++ b/frontend/src/lib/monaco/CodeEditorImpl.tsx
@@ -205,6 +205,12 @@ export function CodeEditor({
// Using useRef, not useState, as we don't want to reload the component when this changes.
const monacoDisposables = useRef([] as IDisposable[])
const mutationObserver = useRef<MutationObserver | null>(null)
+ // Tracks whether the component is still mounted. Because CodeEditor is loaded
+ // through a lazy Suspense facade, Monaco's async `onMount` can fire *after*
+ // React has already torn the component down (quick tab switch / unmount).
+ // Doing DOM work or state updates then throws "can't access dead object" in
+ // Firefox, so `editorOnMount` bails out when this is false.
+ const isMountedRef = useRef(true)
// Track every model this editor instance has been attached to, so we can
// dispose them on unmount. Monaco models live in a global registry until
// explicitly disposed; without this, models accumulate forever and retain
@@ -295,7 +301,13 @@ export function CodeEditor({
}
useOnMountEffect(() => {
+ // Re-arm on (re)mount so a prior cleanup — e.g. React Strict Mode's
+ // mount/unmount/remount in dev — doesn't leave the live component
+ // permanently flagged as unmounted.
+ isMountedRef.current = true
+
return () => {
+ isMountedRef.current = false
disposeMonacoDisposables()
disconnectMutationObserver()
@@ -412,6 +424,19 @@ export function CodeEditor({
}
const editorOnMount = (editor: importedEditor.IStandaloneCodeEditor, monaco: Monaco): void => {
+ // The lazy Suspense facade can resolve after the component has already
+ // unmounted (quick tab switch). Monaco still calls onMount, but touching
+ // DOM or setting state now throws "can't access dead object" in Firefox.
+ // Dispose the orphaned editor and bail before doing any of that.
+ if (!isMountedRef.current) {
+ try {
+ editor.dispose()
+ } catch {
+ // already disposed
+ }
+ return
+ }
+
editorRef.current = editor
trackEditorModels(editor, monaco)
setMonacoAndEditor([monaco, editor])
@@ -440,9 +465,20 @@ export function CodeEditor({
// Monitor for suggestion widget creation and apply styling
const observer = new MutationObserver(() => {
- const suggestWidget = document.querySelector('.monaco-editor .suggest-widget')
- if (suggestWidget) {
- overrideSuggestionWidgetStyling()
+ // If the component unmounted after the observer was installed, stop
+ // querying the (now reclaimed) DOM — this is the Firefox "can't
+ // access dead object" crash. Disconnect defensively and bail.
+ if (!isMountedRef.current) {
+ observer.disconnect()
+ return
+ }
+ try {
+ const suggestWidget = document.querySelector('.monaco-editor .suggest-widget')
+ if (suggestWidget) {
+ overrideSuggestionWidgetStyling()
+ }
+ } catch {
+ observer.disconnect()
}
})
@@ -523,6 +559,16 @@ export function CodeEditor({
if (originalValue) {
// If originalValue is provided, we render a diff editor instead
const diffEditorOnMount = (diff: importedEditor.IStandaloneDiffEditor, monaco: Monaco): void => {
+ // Same lazy-facade race as editorOnMount: bail if we already unmounted.
+ if (!isMountedRef.current) {
+ try {
+ diff.dispose()
+ } catch {
+ // already disposed
+ }
+ return
+ }
+
const modifiedEditor = diff.getModifiedEditor()
editorRef.current = modifiedEditor
diffEditorRef.current = diff