KB Article T419520
Visible to All Users

How to troubleshoot the "Reentrancy or cross thread operation detected" error

Problem

The "Reentrancy or cross thread operation detected" exception occurs when you use a SimpleDataLayer object in multiple threads simultaneously or query additional data during unfinished database operations. While you can temporarily suppress this exception in development with DevExpress.Xpo.SimpleDataLayer.SuppressReentrancyAndThreadSafetyCheck = true, we strongly advise against using this option in production. Instead, please review and fix your code with one of the following solutions.

Solutions

Data manipulations in background threads

  • Switch from SimpleDataLayer to ThreadSafeDataLayer. In XAF .NET 6+ applications, set the options.ThreadSafe parameter of the builder.ObjectSpaceProviders.AddXpo or builder.ObjectSpaceProviders.AddSecuredXpo method from the Startup.cs or ApplicationBuilder.cs file. In XAF .NET Framework applications, set the threadSafe parameter of the XPObjectSpaceProvider or SecuredObjectSpaceProvider object instantiated in the XafApplication.CreateDefaultObjectSpaceProvider method. Note that ThreadSafeDataLayer does not allow you to modify metadata dynamically with XPDictionary.CreateClass, XPClassInfo.CreateMember, and similar methods. To initialize ThreadSafeDataLayer, it is necessary to build metadata in advance using the XPDictionary.CollectClassInfos method. In XAF, please review limitations related to custom persistent fields.
    OR
  • Create a new SimpleDataLayer in a background thread to work with the database safely. For more information, see How to: Connect to a Data Store.
    OR
  • Use async/await XPO API when you want to avoid UI freeze for a better UX. With this API, you will not need to start a background thread. Note that parallel execution of two asynchronous operations should be avoided in the context of one Session instance.

You do not manipulate data in background threads explicitly

Reason #1. Asynchronous data sources load data in a background thread
To avoid this error with XPInstantFeedbackSource and XPInstantFeedbackView components, switch to ThreadSafeDataLayer or create a new SimpleDataLayer instance in the ResolveSession event handler.

Reason #2. Additional data is retrieved during unfinished database operations
Reentrancy errors may even occur with a single thread or ThreadSafeDataLayer when persistent references or collections are loaded until XPO finishes previous database queries - for instance, when you bind a WinForms grid control to XPO data and make database queries in CustomDrawXXX event handlers (based on the WM_Paint message). For more information on these scenarios and solutions, refer to points #3 and #4 at Troubleshooting - How to resolve the "Entering the 'GetObjectsNonReenterant' state…" error.

Reason #3. Your custom or third-party code may use SimpleDataLayer in a background thread indirectly
For instance, you may not find the SimpleDataLayer string in your solution, but you may have XPObjectSpaceProvider or SecuredObjectSpaceProvider references that set threadSafe=false explicitly or implicitly. To locate and troubleshot this problematic code, do the following:

  • Configure your Visual Studio as described at Collect and Analyze the Diagnostic Information and reproduce this error in Visual Studio.
  • Review each stack trace in the Debug | Windows | Threads window. Stack traces with references to SimpleDataLayer will lead to the problematic code that uses a data access layer in a background thread.
  • If diagnostic information shows that this error relates to DevExpress code, attach a small debuggable sample project, describe your use-case scenario in detail, and attach screeenshots of exception callstacks from all threads.

#SuppressReentrancyAndThreadSafetyCheck

Show previous comments (3)
Anatol (DevExpress) 4 years ago

    Hello Walter,

    In order to better serve and track multiple questions in your inquiry, I have taken the liberty of separating the issues you addressed. Please refer to these tickets for further correspondence on these items:
    How to change UnitOfWork's connection string dynamically when using IDataLayer
    Is it recommended to set XpoDefault.DataLayer at startup
    Should I use the ConnectionHelper.Connect method in multithread applications

    E E
    Eric Comte Marois 3 years ago

      Is there any plans for a thread-safe data layer that also supports dynamic metadata? Right now I use SimpleDataLayer and set the deprecated SuppressReentrancyAndThreadSafetyCheck property to true. Whenever this property is finally taken down, I will have a huge problem on my hands.

      Some context: I wrote classes and derived controls that makes it a breeze to create read-only, data bound list controls such as list boxes, grids and combo boxes with only little pieces of informations such as the name of a Sql Server view. They however rely heavily on a shared, long-living SimpleDataLayer instance upon which metadata is added on-the-go, like when a user navigates to a form that requires the information in a particular view to be obtained.

      There's a lot of potential for reusing metadata. Stuff like list of products or customers comes around a lot from one UI to another. For this particular reason, I came to find out it's more efficient to work with just one datalayer and multiple sessions using that same datalayer.

      I wished I could use ThreadSafeDataLayer, but the requirement that every single view used in every single data control in the whole application (hundreds of different UIs) is too steep. I need to keep it dynamic and keep the ability to query the data from a background thread if need be - of course while taking measures and full responsibility to prevent the UI thread from reacting to changes coming from a different thread.

      I'd like a workaround that won't require me to review and rewrite code in hundreds of forms.

      DevExpress Support Team 3 years ago

        Hello David,

        I created a separate ticket on your behalf: T1066262: Is there any plans for a thread-safe data layer that also supports dynamic metadata?. We placed it in our processing queue and will process it shortly.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.