Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindingExpression has not been started #14753

Closed
ltetak opened this issue Feb 28, 2024 · 4 comments · Fixed by #15170
Closed

BindingExpression has not been started #14753

ltetak opened this issue Feb 28, 2024 · 4 comments · Fixed by #15170

Comments

@ltetak
Copy link
Contributor

ltetak commented Feb 28, 2024

Describe the bug

Haven't found the exact place but binding errors do not report properly in some cases and crash the application.

System.InvalidOperationException: BindingExpression has not been started.
   at Avalonia.Data.Core.UntypedBindingExpressionBase.GetValue()
   at Avalonia.Data.Core.UntypedBindingExpressionBase.PublishValue(Object value, BindingError error)
   at Avalonia.Data.Core.BindingExpression.ConvertAndPublishValue(Object value, BindingError error)
   at Avalonia.Data.Core.BindingExpression.OnNodeError(Int32 nodeIndex, String error)
   at Avalonia.Data.Core.ExpressionNodes.ExpressionNode.SetError(String message)
   at Avalonia.Data.Core.ExpressionNodes.LogicalAncestorElementNode.TrackedControlChanged(ILogical control)
   at Avalonia.Reactive.AnonymousObserver`1.OnNext(T value)
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value)
   at Avalonia.LogicalTree.ControlLocator.ControlTracker.Detached(Object sender, LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   at Avalonia.StyledElement.Avalonia.LogicalTree.ILogical.NotifyDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
   at Avalonia.Controls.TopLevel.HandleClosed()
   at Avalonia.Controls.WindowBase.HandleClosed()
   at Avalonia.Controls.Window.HandleClosed()
   at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.WindowImpl.WndProcMessageHandler(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

To Reproduce

Have a binding error.
I guess it has something to do with the lifetime of the control. If the property has not yet been initialized and it errors then it fails.
I could repro it either by opening the app or closing it down.
The case when opening has been fixed by using ReflectionBinding. But this was probably just hiding the issue.

Expected behavior

Don't crash, report the error.

Environment

  • OS: Windows
  • Avalonia-Version: 11.1.999-cibuild0045352-beta - latest master version as of today
@ltetak ltetak added the bug label Feb 28, 2024
@timunie
Copy link
Contributor

timunie commented Feb 28, 2024

Any chance you can share sample code?

@ltetak
Copy link
Contributor Author

ltetak commented Feb 28, 2024

I haven't isolated it from the production app. This is a "pseudocode"

<UserControl ...>
...
        <ItemsControl x:Name="MainPanel" ItemsSource="{Binding $parent[controls:ToolBar].PrimaryItems, Mode=OneTime}">
            ...

I see this binding error in debug just before it crashes

[Binding]An error occurred binding 'ItemsSource' to '$parent[ToolBar].PrimaryItems' at 'PrimaryItems': 'BindingExpression has not been started.' (ItemsControl #MainPanel)

If I change it to ReflectionBinding it does not crash on start and it's even working fine. Something else (or the same thing) crashes when the app closes and detaches controls from the visual tree.

@timunie
Copy link
Contributor

timunie commented Feb 29, 2024

/cc @grokys

@ltetak ltetak mentioned this issue Mar 1, 2024
3 tasks
@TomEdwardsEnscape
Copy link
Contributor

I have a repro case.

https://gist.github.com/TomEdwardsEnscape/6bcc0db015cc4d6f44067a216b116ea4

Apply this patch to the Avalonia repo, start the Sandbox project, then click the button twice.

grokys added a commit that referenced this issue Mar 29, 2024
maxkatz6 pushed a commit that referenced this issue Mar 30, 2024
* Added failing test for #14753.

* Don't try to publish on non-running binding.

This can happen in cases like #14653 where:

1. An event is raised with 2 binding expression subscribers
2. The first subscriber causes the 2nd subscriber to be stopped
3. The second subscriber is called from the event, even though it has been stopped (as the event list was cached at step 1)
4. It calls `PublishValue` causing an exception

Easiest to just do nothing in `PublishValue` when this scenario happens.
maxkatz6 pushed a commit that referenced this issue Apr 20, 2024
* Added failing test for #14753.

* Don't try to publish on non-running binding.

This can happen in cases like #14653 where:

1. An event is raised with 2 binding expression subscribers
2. The first subscriber causes the 2nd subscriber to be stopped
3. The second subscriber is called from the event, even though it has been stopped (as the event list was cached at step 1)
4. It calls `PublishValue` causing an exception

Easiest to just do nothing in `PublishValue` when this scenario happens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants