microsoft/monaco-editor · warning · Error

already initialized

Error message

already initialized

What it means

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.

Source

Thrown at website/src/runner/debug.ts:15

/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { loadMonaco } from "../monaco-loader";
import { IPreviewState } from "../shared";
import { LzmaCompressor } from "../website/utils/lzmaCompressor";
import "./style.scss";

let monacoPromise: Promise<any> | undefined = undefined;

async function initialize(state: IPreviewState) {
	if (monacoPromise) {
		throw new Error("already initialized");
	}

	const loadingContainerDiv = document.createElement("div");
	loadingContainerDiv.className = "loader-container";
	const loadingDiv = document.createElement("div");
	loadingDiv.className = "loader";
	loadingContainerDiv.appendChild(loadingDiv);
	document.body.appendChild(loadingContainerDiv);

	monacoPromise = loadMonaco(state.monacoSetup);
	await monacoPromise;

	loadingContainerDiv.remove();

	const style = document.createElement("style");
	style.id = "custom-style";
	style.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.
	document.body.appendChild(style);

View on GitHub (pinned to ca1b42dc89)

Solutions

  1. On the host side, only post 'initialize' once per iframe load; subsequent state changes should use update-css or reload the iframe.
  2. If re-initialization is genuinely needed, reload the iframe (set src or use location.reload) so the runner starts fresh.
  3. In development, disable React StrictMode double-invoke for the effect that posts initialize, or guard it with a ref.
  4. Debounce/dedupe initialize messages on the sender.

Example fix

// before — host posts initialize on every preview change
window.runner.contentWindow.postMessage({ kind: 'initialize', state }, '*');
// after — only initialize once, reload iframe for fresh state
if (!runnerInitialized.current) {
  runnerRef.current.contentWindow.postMessage({ kind: 'initialize', state }, '*');
  runnerInitialized.current = true;
} else {
  runnerRef.current.src = runnerRef.current.src; // force reload then initialize
}
Defensive patterns

Strategy: validation

Validate before calling

// on the host: only post 'initialize' once per iframe load
if (!runnerInitializedRef.current) {
  runnerRef.current!.contentWindow!.postMessage({ kind: 'initialize', state }, '*');
  runnerInitializedRef.current = true;
} else if (needsFullReinit) {
  runnerRef.current!.src = runnerRef.current!.src; // reload, then post initialize again
  runnerInitializedRef.current = false;
}

Type guard

function runnerAcceptsInitialize(hasInitialized: boolean): boolean {
  return !hasInitialized;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/monaco-editor@ca1b42dc89 (2026-08-13). Data as JSON: /api/errors/2dd78ef5d3889690. Report an issue: GitHub.