Editor: "InstantiationService has been disposed" from deferred SuggestWidget creation when multiple editors are created/disposed on one page (global hoverDelegateFactory singleton)

Open
#5,518 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@hediet is already working on this.

Since Oct 9, 2026.

Assessment

This issue has not been assessed yet.

Description

Summary

In an Electron desktop app that creates and disposes several Monaco editors on one page (read-only diff editors + file viewers), we captured an uncaught renderer error:

Error: InstantiationService has been disposed

thrown while SuggestWidget was being constructed — not for a user action, but from a deferred WindowIdleValue executor that ran after the editor had already been disposed.

The underlying defect is that hoverDelegateFactory is a module-global mutable singleton (with Lazy caches), and every StandaloneCodeEditor constructor overwrites it with a closure bound to that editor's own InstantiationService. When the last-created editor is disposed, the global factory keeps pointing at the disposed service, and any later widget construction that goes through createInstantHoverDelegate() throws. This is the same root cause described in microsoft/vscode#279709, manifesting here through the suggest-widget path (microsoft/monaco-editor#4612 captured the same error via the context-menu path).

Environment
  • monaco-editor 0.56.0 (npm, standalone editors via monaco.editor.create and monaco.editor.createDiffEditor)
  • Electron 43 / Chromium 150, macOS arm64 (also plausible in any browser host)
  • Multiple editors per page, created and disposed as the user navigates
  • The error is uncaught because it is thrown inside a requestIdleCallback callback
Stack trace (from production, only Monaco frames)
Error: InstantiationService has been disposed
  at InstantiationService._throwIfDisposed   instantiationService.js:52:19
  at InstantiationService.createInstance     instantiationService.js:93:14
  at <anonymous> (global hoverDelegateFactory closure)   standaloneCodeEditor.js:81:89
  at createInstantHoverDelegate              hoverDelegateFactory.js:31:12
  at new ActionBar                           actionbar.js:40:71
  at new SuggestWidgetStatus                 suggestWidgetStatus.js:37:29
  at new SuggestWidget                       suggestWidget.js:214:45
  at InstantiationService.createInstance     instantiationService.js:102:27
  at SuggestController constructor (inside WindowIdleValue executor)   suggestController.js:131:55
  -- async boundary: scheduled by WindowIdleValue/runWhenIdle (requestIdleCallback)   async.js:636
How it happened (sequence from production logs)
  1. Several Monaco editors existed on the page; each creation ran setHoverDelegateFactory(...) in StandaloneCodeEditor's constructor (line 81 in 0.56.0), so the global factory ended up bound to the last-created editor's InstantiationService.
  2. The user interacted with a (read-only) editor for the first time. SuggestController is registered with EditorContributionInstantiation.BeforeFirstInteraction, so it was constructed at that moment. Its constructor defers SuggestWidget creation:
    this.widget = this._toDispose.add(new WindowIdleValue(getWindow(editor.getDomNode()), () => {
        const widget = this._instantiationService.createInstance(SuggestWidget, this.editor);
        ...
    }));
    
  3. The renderer's main thread stayed busy (a background task streamed updates ~4×/second and re-rendered a large DOM), so the idle callback did not fire.
  4. The user navigated; React unmounted the view and editor.dispose() ran. Editor creation/disposal had also swapped which editor owns the global factory; the owning service was disposed.
  5. The renderer finally went idle → the pending idle callback fired → new ActionBar → createInstantHoverDelegate() → the stale global closure → createInstance on a disposed InstantiationService → uncaught throw.

Note the two compounding problems:

  • A: the global hoverDelegateFactory singleton is bound to one specific editor's InstantiationService and silently goes stale when that editor is disposed (never reset to nullHoverDelegateFactory). Every other still-alive editor inherits the stale factory.
  • B: deferred widget construction (WindowIdleValue in SuggestController) executes createInstance without any guard for "editor/service already disposed in the meantime". Once the idle callback is dequeued, cancelIdleCallback can no longer prevent it, so contribution disposal does not reliably stop it.
Minimal repro sketch
const a = monaco.editor.create(elA, { value: 'const x = 1;', language: 'javascript' });
// Creating a second editor overwrites the global hoverDelegateFactory with B's InstantiationService:
const b = monaco.editor.createDiffEditor(elB, { originalEditable: false, readOnly: true });
// First interaction with `a` creates SuggestController (BeforeFirstInteraction)
// and schedules SuggestWidget construction on requestIdleCallback. Keep the main
// thread busy (or dispose quickly) so the idle callback is still pending, then:
b.dispose();          // global factory now points at B's disposed InstantiationService
// When the idle callback fires: SuggestWidget -> SuggestWidgetStatus -> ActionBar
// -> createInstantHoverDelegate() -> stale factory -> Error: InstantiationService has been disposed

Timing-sensitive by nature (requires the idle callback to fire after the dispose), which is why it surfaces rarely and non-deterministically.

Related issues
  • microsoft/vscode#279709 — root-cause analysis of the global hoverDelegateFactory singleton being overwritten by createDiffEditor/createEditor (same standaloneCodeEditor.js frame, FindWidget path)
  • microsoft/monaco-editor#4612 — same error message in Monaco via ContextMenuController → ActionBar → createInstantHoverDelegate
  • microsoft/vscode#229026 — same class of error via ActionBar → createActionViewItem after dispose
Suggested fixes
  1. Remove the module-global factory — resolve the hover delegate per-editor (e.g., pass/look up the requesting editor's InstantiationService instead of a singleton closure). This fixes every consumer (ActionBar, FindInput, context menu, suggest widget) at once.
  2. Alternatively, register the factory's lifecycle with the owning editor so that disposing it resets the global to nullHoverDelegateFactory (fail fast instead of throwing from a stale closure).
  3. Additionally/defensively: make deferred WindowIdleValue executors in editor contributions dispose-safe — check editor.hasBeenDisposed/service state before constructing the widget, so late idle callbacks become no-ops instead of uncaught errors.

Option 1 is the real fix; option 3 additionally hardens all lazily-constructed contributions against the same timing window.

Dominant language
JavaScript
Stars
47k
Forks
4.2k
Avg merge
3d 13h
Merged PRs (30d)
13

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/monaco-editor

All issues in microsoft/monaco-editor

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.