AvaloniaUI / AvaloniaEdit

Avalonia-based text editor (port of AvalonEdit)
MIT License
785 stars 150 forks source link

System.InvalidOperationException: Operation is not valid due to the current state of the object under Linux Ubuntu WSL #338

Open martin-honnen opened 1 year ago

martin-honnen commented 1 year ago

I have a project using the release candidate that has a menu item trying to set the Text property of an editor to some string. For some reasons that works on some tries but later fails with Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object. under Ubuntu 22.04 WLS.

The full stack trace is below.

I have not been able to figure why using the menu item to set the editor's Text works sometimes and then fails with that exception.

Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at AvaloniaEdit.Document.DocumentLine.get_LineNumber()
   at AvaloniaEdit.Editing.LineNumberMargin.Render(DrawingContext drawingContext)
   at Avalonia.Rendering.Composition.CompositingRenderer.UpdateCore() in /_/src/Avalonia.Base/Rendering/Composition/CompositingRenderer.cs:line 276
   at Avalonia.Rendering.Composition.CompositingRenderer.Update() in /_/src/Avalonia.Base/Rendering/Composition/CompositingRenderer.cs:line 318
   at Avalonia.Rendering.Composition.Compositor.CommitCore() in /_/src/Avalonia.Base/Rendering/Composition/Compositor.cs:line 136
   at Avalonia.Rendering.Composition.Compositor.Commit() in /_/src/Avalonia.Base/Rendering/Composition/Compositor.cs:line 126
   at Avalonia.Media.MediaContext.CommitCompositor(Compositor compositor) in /_/src/Avalonia.Base/Media/MediaContext.Compositor.cs:line 25
   at Avalonia.Media.MediaContext.SyncCommit(Compositor compositor, Boolean waitFullRender) in /_/src/Avalonia.Base/Media/MediaContext.Compositor.cs:line 112
   at Avalonia.Media.MediaContext.SyncDisposeCompositionTarget(CompositionTarget compositionTarget) in /_/src/Avalonia.Base/Media/MediaContext.Compositor.cs:line 134
   at Avalonia.Rendering.Composition.CompositingRenderer.Dispose() in /_/src/Avalonia.Base/Rendering/Composition/CompositingRenderer.cs:line 368
   at Avalonia.Controls.TopLevel.HandleClosed() in /_/src/Avalonia.Controls/TopLevel.cs:line 583
   at Avalonia.Controls.WindowBase.HandleClosed() in /_/src/Avalonia.Controls/WindowBase.cs:line 232
   at Avalonia.X11.X11Window.Cleanup(Boolean fromDestroyNotification) in /_/src/Avalonia.X11/X11Window.cs:line 868
   at Avalonia.X11.X11Window.Dispose() in /_/src/Avalonia.X11/X11Window.cs:line 826
   at Avalonia.Controls.Primitives.PopupRoot.Dispose() in /_/src/Avalonia.Controls/Primitives/PopupRoot.cs:line 93
   at Avalonia.Controls.Primitives.Popup.<>c.<Open>b__93_6(ValueTuple`2 state) in /_/src/Avalonia.Controls/Primitives/Popup.cs:line 460
   at Avalonia.Reactive.Disposable.AnonymousDisposable`1.Dispose() in /_/src/Avalonia.Base/Reactive/Disposable.cs:line 56
   at Avalonia.Controls.Primitives.Popup.CloseCore() in /_/src/Avalonia.Controls/Primitives/Popup.cs:line 718
   at Avalonia.Controls.Primitives.Popup.IsOpenChanged(AvaloniaPropertyChangedEventArgs`1 e) in /_/src/Avalonia.Controls/Primitives/Popup.cs:line 679
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value) in /_/src/Avalonia.Base/Reactive/LightweightObservableBase.cs:line 139
   at Avalonia.PropertyStore.EffectiveValue`1.SetAndRaiseCore(ValueStore owner, StyledProperty`1 property, T value, BindingPriority priority, Boolean isOverriddenCurrentValue, Boolean isCoercedDefaultValue) in /_/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs:line 243
   at Avalonia.PropertyStore.EffectiveValue`1.SetAndRaise(ValueStore owner, IValueEntry value, BindingPriority priority) in /_/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs:line 67
   at Avalonia.PropertyStore.ValueStore.ReevaluateEffectiveValue(AvaloniaProperty property, EffectiveValue current, IValueEntry changedValueEntry, Boolean ignoreLocalValue) in /_/src/Avalonia.Base/PropertyStore/ValueStore.cs:line 850
   at Avalonia.PropertyStore.ValueStore.OnBindingValueChanged(IValueEntry entry, BindingPriority priority) in /_/src/Avalonia.Base/PropertyStore/ValueStore.cs:line 414
   at Avalonia.PropertyStore.BindingEntryBase`2.<SetValue>g__Execute|38_0(BindingEntryBase`2 instance, BindingValue`1 value) in /_/src/Avalonia.Base/PropertyStore/BindingEntryBase.cs:line 138
   at Avalonia.PropertyStore.BindingEntryBase`2.SetValue(BindingValue`1 value) in /_/src/Avalonia.Base/PropertyStore/BindingEntryBase.cs:line 181
   at Avalonia.Data.TemplateBinding.PublishValue() in /_/src/Avalonia.Base/Data/TemplateBinding.cs:line 183
   at Avalonia.PropertyStore.EffectiveValue`1.SetAndRaiseCore(ValueStore owner, StyledProperty`1 property, T value, BindingPriority priority, Boolean isOverriddenCurrentValue, Boolean isCoercedDefaultValue) in /_/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs:line 243
   at Avalonia.PropertyStore.ValueStore.SetLocalValue[T](StyledProperty`1 property, T value) in /_/src/Avalonia.Base/PropertyStore/ValueStore.cs:line 217
   at Avalonia.PropertyStore.ValueStore.SetValue[T](StyledProperty`1 property, T value, BindingPriority priority) in /_/src/Avalonia.Base/PropertyStore/ValueStore.cs:line 170
   at Avalonia.AvaloniaObject.SetValue[T](StyledProperty`1 property, T value, BindingPriority priority) in /_/src/Avalonia.Base/AvaloniaObject.cs:line 325
   at Avalonia.Controls.MenuItem.set_IsSubMenuOpen(Boolean value) in /_/src/Avalonia.Controls/MenuItem.cs:line 270
   at Avalonia.Controls.MenuItem.CloseSubmenus() in /_/src/Avalonia.Controls/MenuItem.cs:line 521
   at Avalonia.Controls.MenuItem.SubMenuOpenChanged(AvaloniaPropertyChangedEventArgs e) in /_/src/Avalonia.Controls/MenuItem.cs:line 674
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value) in /_/src/Avalonia.Base/Reactive/LightweightObservableBase.cs:line 139
   at Avalonia.PropertyStore.EffectiveValue`1.SetAndRaiseCore(ValueStore owner, StyledProperty`1 property, T value, BindingPriority priority, Boolean isOverriddenCurrentValue, Boolean isCoercedDefaultValue) in /_/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs:line 243
   at Avalonia.PropertyStore.ValueStore.SetCurrentValue[T](StyledProperty`1 property, T value) in /_/src/Avalonia.Base/PropertyStore/ValueStore.cs:line 203
   at Avalonia.AvaloniaObject.SetCurrentValue[T](StyledProperty`1 property, T value) in /_/src/Avalonia.Base/AvaloniaObject.cs:line 394
   at Avalonia.Controls.MenuItem.Close() in /_/src/Avalonia.Controls/MenuItem.cs:line 337
   at Avalonia.Controls.Menu.Close() in /_/src/Avalonia.Controls/Menu.cs:line 53
   at Avalonia.Controls.Platform.DefaultMenuInteractionHandler.Click(IMenuItem item) in /_/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs:line 359
   at Avalonia.Controls.Platform.DefaultMenuInteractionHandler.PointerReleased(Object sender, PointerReleasedEventArgs e) in /_/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs:line 238
   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e) in /_/src/Avalonia.Base/Interactivity/EventRoute.cs:line 149
   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e) in /_/src/Avalonia.Base/Interactivity/EventRoute.cs:line 79
   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) in /_/src/Avalonia.Base/Interactivity/Interactive.cs:line 126
   at Avalonia.Input.MouseDevice.MouseUp(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties props, KeyModifiers inputModifiers, IInputElement hitTest) in /_/src/Avalonia.Base/Input/MouseDevice.cs:line 200
   at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e) in /_/src/Avalonia.Base/Input/MouseDevice.cs:line 58
   at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e) in /_/src/Avalonia.Base/Input/InputManager.cs:line 37
   at Avalonia.Controls.TopLevel.HandleInput(RawInputEventArgs e) in /_/src/Avalonia.Controls/TopLevel.cs:line 739
   at Avalonia.X11.X11Window.DispatchInput(RawInputEventArgs args) in /_/src/Avalonia.X11/X11Window.cs:line 747
   at Avalonia.RawEventGrouper.Dispatch(RawInputEventArgs ev) in /_/src/Shared/RawEventGrouping.cs:line 127
   at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl) in /_/src/Avalonia.Base/Threading/DispatcherFrame.cs:line 117
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame) in /_/src/Avalonia.Base/Threading/Dispatcher.MainLoop.cs:line 53
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.MainLoop.cs:line 80
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 127
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 220
   at SaxonCSAvaloniaXMLWorkbench.Program.Main(String[] args) in C:\Users\marti\source\repos\SaxonCSAvaloniaXMLWorkbench\SaxonCSAvaloniaXMLWorkbench\Program.cs:line 13
danipen commented 1 year ago

It seems the compositor is calling from a different thread the control was created. Crashing here:

https://github.com/AvaloniaUI/AvaloniaEdit/blob/33d7342e00f5b94f72ce6ddbaea552b458268208/src/AvaloniaEdit/Document/TextDocument.cs#L155

Cc/ @Gillibald @kekekeks