LykosAI/StabilityMatrix · error · InvalidOperationException

Cannot create a marker when not attached to a document

Error message

Cannot create a marker when not attached to a document

What it means

TextMarkerService.TryCreate requires the service to be attached to a text document (an AvalonEdit-style document/text area). When _markers is null — meaning the service never attached — it throws InvalidOperationException('Cannot create a marker when not attached to a document') instead of silently returning null.

Solutions

  1. Guard calls so TryCreate is only invoked after the TextArea.Document is set and the service is attached
  2. Check an IsAttached/_markers != null condition before calling TryCreate
  3. Re-attach (or recreate) the service when a new document is loaded
  4. Return null or queue pending markers until attachment instead of calling early

Example fix

// before
var marker = _markerService.TryCreate(offset, length);
// after
if (_markerService is { } svc && textArea.Document is not null)
    var marker = svc.TryCreate(offset, length);
Defensive patterns

Strategy: try-catch

Validate before calling

if (textArea?.Document is null) return null; // not attached yet

Type guard

bool IsAttached(TextMarkerService s) => s.GetType().GetField("_markers", BindingFlags.NonPublic|BindingFlags.Instance)?.GetValue(s) != null;

Try / catch

try { marker = service.TryCreate(offset, length); }
catch (InvalidOperationException) { marker = null; /* queue for after attach */ }

Prevention

When it happens

Trigger: Calling TryCreate before the service is attached to a TextArea/Document (e.g. during editor construction or after the editor was closed and detached), or creating markers from a background thread before attachment completes.

Common situations: Diagnostics/ squiggles pipeline running before the editor finishes initializing; markers requested after document unload; view model calls into the service when the editor control hasn't loaded.

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 LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/eb5079b20a57e393. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Controls/TextMarkers/TextMarkerService.cs:76

        //    {
        //        args.SetToolTip(GetTooltipTextForCollapsedSection(args, collapsedSection));
        //    }
        //}

        var markersAtOffset = GetMarkersAtOffset(offset);
        var markerWithToolTip = markersAtOffset.FirstOrDefault(marker => marker.ToolTip != null);
        if (markerWithToolTip != null && markerWithToolTip.ToolTip != null)
        {
            args.SetToolTip(markerWithToolTip.ToolTip);
        }
    }*/

    #region TextMarkerService

    public TextMarker? TryCreate(int startOffset, int length)
    {
        if (_markers == null)
            throw new InvalidOperationException("Cannot create a marker when not attached to a document");

        var textLength = _document.TextLength;
        if (startOffset < 0 || startOffset > textLength) return null;
        //throw new ArgumentOutOfRangeException(nameof(startOffset), startOffset, "Value must be between 0 and " + textLength);
        if (length < 0 || startOffset + length > textLength) return null;
        //throw new ArgumentOutOfRangeException(nameof(length), length, "length must not be negative and startOffset+length must not be after the end of the document");

        var marker = new TextMarker(this, startOffset, length);
        _markers.Add(marker);
        return marker;
    }

    public IEnumerable<TextMarker> GetMarkersAtOffset(int offset)
    {
        return _markers.FindSegmentsContaining(offset);
    }

    public IEnumerable<TextMarker> TextMarkers => _markers ?? Enumerable.Empty<TextMarker>();

View on GitHub (pinned to af93d6ef57)