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

Dynamically adding new nodes causes exceptions #466

Open
cwendjustus opened this issue Aug 12, 2024 · 4 comments
Open

Dynamically adding new nodes causes exceptions #466

cwendjustus opened this issue Aug 12, 2024 · 4 comments

Comments

@cwendjustus
Copy link

I've noticed adding/removing nodes/ports can cause exceptions due to race conditions. There are three cases I have documented (of which one is the Navigator Widget) but everything which makes use of the changed collections and is triggered by a re-render can cause the same exceptions to be thrown.

Note: these are lines copied from a debugging session

NavigatorWidget.cs
	foreach (... in BlazorDiagram.Nodes ...) EXCEPTION (collection was modified)
DiagramCanvas.cs
	foreach (... in BlazorDiagram.OrderedSelectables) EXCEPTION (collection was modified)
JSRuntimeExtensions.cs
	return await Microsoft.JSInterop.JSRuntimeExtensions.InvokeAsync<Rectangle>(jsRuntime, "ZBlazorDiagrams.getBoundingClientRect", new object[1] { element }); EXCEPTION (some object is null)

I think the cause is the automatic re-rendering on diagram changes while modifying collections (the nodes and/or links).
My first attempt at resolving this was setting both Diagram.SuspendRefresh and Diagram.SuspendSorting to false but this does not solve the issue.

Has anyone else experienced this issue or have suggestions?

@zHaytam
Copy link
Collaborator

zHaytam commented Aug 12, 2024

Hello, do you maybe have a way to reproduce this consistently? That would help me fix it.

@cwendjustus
Copy link
Author

I'm afraid I cannot provide a complete example easily. I'll give some context though.
There's a view rendering the DiagramCanvas component. When some data unrelated to the view changes, the following happens to the diagram:

  • nodes and links are cleared
  • new nodes are added (in a for loop, each with Nodes.Add)
  • new links are created

If I'm not mistaken, these actions trigger events which cause re-rendering of the DiagramCanvas component. So the errors make sense.
I think adding a mutex for objects that can be accessed (and written to) by multiple threads at the same time would do the trick.

@cwendjustus
Copy link
Author

The problem originates from a ReactiveUI event modifying the diagram content while it is still rendering the previous change.
Adding mutexes/locks in the code would be a mess, but adding a feature like described in #410 in the form of an event would be a nice alternative.
Although I have no clue how something like that could be easily (and consistently) implemented with all the Actions and async programming going on.

@zHaytam
Copy link
Collaborator

zHaytam commented Aug 18, 2024

Yeah, it's tough to know exactly when the rendering finished, since a few JS calls need to be completed first.
I will try to see if I can do something about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants