{"record":{"id":"2dd78ef5d3889690","repo":"microsoft/monaco-editor","slug":"already-initialized","errorCode":null,"errorMessage":"already initialized","messagePattern":"already initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"website/src/runner/debug.ts","lineNumber":15,"sourceCode":"/*---------------------------------------------------------------------------------------------\n *  Copyright (c) Microsoft Corporation. All rights reserved.\n *  Licensed under the MIT License. See License.txt in the project root for license information.\n *--------------------------------------------------------------------------------------------*/\n\nimport { loadMonaco } from \"../monaco-loader\";\nimport { IPreviewState } from \"../shared\";\nimport { LzmaCompressor } from \"../website/utils/lzmaCompressor\";\nimport \"./style.scss\";\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":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/microsoft/monaco-editor/blob/ca1b42dc896b99db78e1c743256e8654e12cea2d/website/src/runner/debug.ts#L1-L33","documentation":"Thrown by the debug runner's initialize() when monacoPromise is already set, i.e. initialize() has been called once already in this iframe's lifetime. The debug runner is a sandboxed iframe that loads Monaco once and replays a preview state; a second 'initialize' message would double-load Monaco and re-run setup, so the guard rejects it. The runner is single-use per navigation.","triggerScenarios":"The parent window posts two 'initialize' messages to the same debug-runner iframe without reloading it; an HMR/reload that re-dispatches a queued initialize message; a bug in the host dispatching initialize on every state change instead of only on first load.","commonSituations":"Playground host re-sends initialize when switching into debug mode; React strict-mode double-invoking an effect that posts the initialize message; a stale message queue replayed after navigation.","solutions":["On the host side, only post 'initialize' once per iframe load; subsequent state changes should use update-css or reload the iframe.","If re-initialization is genuinely needed, reload the iframe (set src or use location.reload) so the runner starts fresh.","In development, disable React StrictMode double-invoke for the effect that posts initialize, or guard it with a ref.","Debounce/dedupe initialize messages on the sender."],"exampleFix":"// before — host posts initialize on every preview change\nwindow.runner.contentWindow.postMessage({ kind: 'initialize', state }, '*');\n// after — only initialize once, reload iframe for fresh state\nif (!runnerInitialized.current) {\n  runnerRef.current.contentWindow.postMessage({ kind: 'initialize', state }, '*');\n  runnerInitialized.current = true;\n} else {\n  runnerRef.current.src = runnerRef.current.src; // force reload then initialize\n}","handlingStrategy":"validation","validationCode":"// on the host: only post 'initialize' once per iframe load\nif (!runnerInitializedRef.current) {\n  runnerRef.current!.contentWindow!.postMessage({ kind: 'initialize', state }, '*');\n  runnerInitializedRef.current = true;\n} else if (needsFullReinit) {\n  runnerRef.current!.src = runnerRef.current!.src; // reload, then post initialize again\n  runnerInitializedRef.current = false;\n}","typeGuard":"function runnerAcceptsInitialize(hasInitialized: boolean): boolean {\n  return !hasInitialized;\n}","tryCatchPattern":null,"preventionTips":["Track initialization with a ref/flag so re-renders don't double-post.","For full re-init, reload the iframe rather than re-posting initialize.","Use update-css for CSS-only changes; reserve initialize for first load."],"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"}