ssssssss-team/spider-flow · error · Error

This document is already in use.

Error message

This document is already in use.

What it means

Generic sentinel error in CodeMirror's document model: an operation (typically attaching a document to a second editor or creating a linked doc copy) targeted a Doc whose cm property is already set, meaning the document is already owned by an editor instance. CodeMirror deliberately forbids sharing one Doc between two editors unless it is explicitly linked, so the guard throws instead of silently corrupting editor state.

Solutions

  1. Call CodeMirror(doc.copy()) or doc.linkedDoc() to get an independent copy for the second editor
  2. Detach first: swapDoc a placeholder doc into the old editor before attaching elsewhere
  3. Keep one editor per Doc and re-create the Doc from the saved value/text instead

Example fix

// before
otherEditor.swapDoc(sharedDoc); // sharedDoc still in use
// after
otherEditor.swapDoc(CodeMirror.Doc(sharedDoc.getValue(), sharedDoc.modeOption));
Defensive patterns

Strategy: validation

Validate before calling

function canAttach(doc) {
  return doc && !doc.cm; // doc.cm is set while attached to an editor
}
if (!canAttach(doc)) doc = doc.copy(); // or CodeMirror.Doc(doc.getValue())

Type guard

function isFreeDoc(doc) {
  return doc instanceof CodeMirror.Doc && doc.cm == null;
}

Try / catch

try {
  cm.swapDoc(doc);
} catch (e) {
  if (/already in use/.test(e.message)) {
    cm.swapDoc(doc.linkedDoc({sharedHist: true}));
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling cm.swapDoc(doc) or new CodeMirror(el, {value: doc}) / CodeMirror(el, doc) with a Doc that is already attached to another (live) editor, or re-attaching the same doc to the same editor.

Common situations: Sharing one Doc between split-pane editors, reusing a doc stored on a closed-but-not-detached editor, or restoring editor state from a cache where docs weren't detached.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of ssssssss-team/spider-flow@c799cca99c (2026-09-08). Data as JSON: /api/errors/376b82be6391f4f6. Report an issue: GitHub.

Appendix: source

Thrown at spider-flow-web/src/main/resources/static/js/codemirror/codemirror.js:4765

  // Call f for all linked documents.
  function linkedDocs(doc, f, sharedHistOnly) {
    function propagate(doc, skip, sharedHist) {
      if (doc.linked) { for (var i = 0; i < doc.linked.length; ++i) {
        var rel = doc.linked[i];
        if (rel.doc == skip) { continue }
        var shared = sharedHist && rel.sharedHist;
        if (sharedHistOnly && !shared) { continue }
        f(rel.doc, shared);
        propagate(rel.doc, doc, shared);
      } }
    }
    propagate(doc, null, true);
  }

  // Attach a document to an editor.
  function attachDoc(cm, doc) {
    if (doc.cm) { throw new Error("This document is already in use.") }
    cm.doc = doc;
    doc.cm = cm;
    estimateLineHeights(cm);
    loadMode(cm);
    setDirectionClass(cm);
    if (!cm.options.lineWrapping) { findMaxLine(cm); }
    cm.options.mode = doc.modeOption;
    regChange(cm);
  }

  function setDirectionClass(cm) {
  (cm.doc.direction == "rtl" ? addClass : rmClass)(cm.display.lineDiv, "CodeMirror-rtl");
  }

  function directionChanged(cm) {
    runInOp(cm, function () {
      setDirectionClass(cm);
      regChange(cm);

View on GitHub (pinned to c799cca99c)