OpenRIAServices / OpenRiaServices

The Open RIA Services project continues what was previously known as WCF RIA Services.
https://openriaservices.gitbook.io/openriaservices/
Apache License 2.0
54 stars 47 forks source link

Error updating 2 entities #490

Closed dkarne closed 2 months ago

dkarne commented 4 months ago

Describe the bug I have a large project with several Entities. When we try to update 2 entities at the same time the Update fails with the following result

PropertyNames = '(new System.Linq.SystemCore_EnumerableDebugView(submitOp.EntitiesInError).Items[0]).EntityConflict.PropertyNames' threw an exception of type 'System.InvalidOperationException'

Entity A and Entity B are updated at the same time. Entity B is a child of Entity A.

If I update Entity A and then update Entity B, the problem is not present. This only happens when Entity A and Entity B are being updated and no other previous operation was performed.

Expected behavior Both Entities are properly updated without errors

Details

Visual Studio 2022, EF 6, Open Ria 5

Daniel-Svensson commented 3 months ago

@dkarne Are you doing the modification concurrently or one after the other ? We also need to know the exact version of OpenRiaServices used. Are you using the latest 5.4.3 or something else?

If you are making concurrent changes

OpenRiaServices is not thread safe in general so it is generally not safe to make concurrent updates on different threads.

You can however (in contrast with ef) do several concurrent loads as long as you

  1. Start them from the same thread
  2. that have an active SyncronizationContext.Current which limit conccurrency to 1 (this means it works on the "UI-thread" on wpf/winforms etc.)

If you make changes on the same thread

If your scenario is different and you modify 2 entities, one after the other, then it sounds like it can be a bug and I would need a small repro / example code. Ideally you would be able to add a test case to the "OpenRiaServices.Client.Tests" project, but any kind of repro would help

dkarne commented 3 months ago

Hi Daniel, thanks for your reply. Where should I set this SyncronizationContext?

We are using 5.4.0 right now

I did a lot of changes and now I'm getting this bug.

We load around 20 Entities and after logging in we save some data. This is the message we get when saving the data. Is just 1 table with changes.

Submit operation failed. The underlying provider failed on Open. InnerException message: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. [System.Exception] Submit operation failed. The underlying provider failed on Open. InnerException message: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

[OpenRiaServices.Client.SubmitOperationException] Submit operation failed. The underlying provider failed on Open. InnerException message: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. at System.Data.Entity.Core.EntityClient.EntityConnection.d__56.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Daniel-Svensson commented 2 months ago

@dkarne based on the error message it seems like the error message is related with EF Core and is not related to OpenRiaServices so I will close the issue.

Here are some hints of how you can resolve it.

You can search for A second operation started on this context before a previous asynchronous operation completed As mentioned, efcore does not support multiple concurrent databases accesses (as they write in the exception message "se 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.")

General things to verify

I did a quick search and you might find the following relevant: