{"record":{"id":"f359481034d3705b","repo":"microsoft/monaco-editor","slug":"already-initialized-f35948","errorCode":null,"errorMessage":"already initialized","messagePattern":"already initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"website/src/runner/index.ts","lineNumber":32,"sourceCode":"\t\tconsole.error(\"not in sandbox\");\n\t\treturn;\n\t}*/\n\tconst e = event.data as IMessageToRunner | { kind: undefined };\n\tif (e.kind === \"initialize\") {\n\t\tinitialize(e.state);\n\t} else if (e.kind === \"update-css\") {\n\t\tconst style = document.getElementById(\n\t\t\t\"custom-style\"\n\t\t) as HTMLStyleElement;\n\t\tstyle.innerHTML = e.css; // CodeQL [SM03712] This is safe because the runner runs in an isolated iframe.\n\t}\n});\n\nlet monacoPromise: Promise<any> | undefined = undefined;\n\nasync function initialize(state: IPreviewState) {\n\tif (monacoPromise) {\n\t\tthrow new Error(\"already initialized\");\n\t}\n\n\tconst loadingContainerDiv = document.createElement(\"div\");\n\tloadingContainerDiv.className = \"loader-container\";\n\tconst loadingDiv = document.createElement(\"div\");\n\tloadingDiv.className = \"loader\";\n\tloadingContainerDiv.appendChild(loadingDiv);\n\tdocument.body.appendChild(loadingContainerDiv);\n\n\tmonacoPromise = loadMonaco(state.monacoSetup);\n\tawait monacoPromise;\n\n\tloadingContainerDiv.remove();\n\n\tconst style = document.createElement(\"style\");\n\tstyle.id = \"custom-style\";\n\tstyle.innerHTML = state.css; // CodeQL [SM03712] This is safe because the runner runs in an isolated iframe. This feature is essential to the functionality of the playground. // CodeQL [SM02688] This is safe because the runner runs in an isolated iframe. This feature is essential to the functionality of the playground.\n\tdocument.body.appendChild(style);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/microsoft/monaco-editor/blob/ca1b42dc896b99db78e1c743256e8654e12cea2d/website/src/runner/index.ts#L14-L50","documentation":"Thrown by the main runner's initialize() when monacoPromise is already defined, i.e. the runner iframe has already been initialized in its current lifetime. The main runner loads Monaco once, injects CSS/HTML, and evals the playground JS; a second initialize would re-load Monaco and re-eval, so the guard rejects the duplicate 'initialize' message. The runner is designed to be reloaded, not re-initialized in place.","triggerScenarios":"Parent window sends a second 'initialize' message to the same runner iframe without reloading it; an effect that re-posts initialize on every render; HMR replaying the message; the playground host dispatching initialize when only an update-css was needed.","commonSituations":"React/Vue host that posts initialize inside an effect without a guard; StrictMode double-invoke; state-management code that re-runs the full init flow on unrelated state changes.","solutions":["Send 'initialize' to the runner exactly once per iframe load; use 'update-css' for CSS-only changes.","For a full re-init, reload the iframe (reset src or location.reload()) before posting initialize.","Guard the postMessage call with a ref/flag on the host so re-renders don't duplicate it.","Disable StrictMode double-invoke for the initializing effect, or make the effect idempotent by checking iframe readiness."],"exampleFix":"// before — posts initialize on every render\nuseEffect(() => {\n  iframe.contentWindow.postMessage({ kind: 'initialize', state }, '*');\n});\n// after\nconst sent = useRef(false);\nuseEffect(() => {\n  if (sent.current) return;\n  iframe.contentWindow.postMessage({ kind: 'initialize', state }, '*');\n  sent.current = true;\n}, [state]);","handlingStrategy":"validation","validationCode":"// host-side: post initialize exactly once per iframe load\nconst initialized = useRef(false);\nuseEffect(() => {\n  if (initialized.current) return;\n  iframeRef.current!.contentWindow!.postMessage({ kind: 'initialize', state }, '*');\n  initialized.current = true;\n}, [state]);\n// for a fresh start, reset iframe.src and clear the flag","typeGuard":"function shouldInitialize(hasInitialized: boolean): boolean {\n  return !hasInitialized;\n}","tryCatchPattern":null,"preventionTips":["Guard the postMessage('initialize') call with a per-iframe flag.","Send update-css for styling changes; reload the iframe for a full reset.","Watch out for React StrictMode double-invoke of effects in development."],"tags":["runtime","website","runner","iframe","initialization"],"backgroundTag":null,"analyzedSha":"ca1b42dc896b99db78e1c743256e8654e12cea2d","analyzedAt":"2026-08-13T02:00:51.380Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}