question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Concurrency Issue

See original GitHub issue

Code USed: Startup.cs

       DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); 

       var userAgent = context.Request.Headers["User-Agent"]; 

        var deviceDetector = new DeviceDetector(userAgent); 

        deviceDetector.Parse(); 

Issue encounter: System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection’s state is no longer correct.

at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)

at System.Collections.Generic.Dictionary`2.ContainsKey(TKey key)

at DeviceDetectorNET.Cache.DictionaryCache.Contains(String id)

at DeviceDetectorNET.Cache.DictionaryCache.Fetch(String id)

at DeviceDetectorNET.Parser.ParserAbstract`2.GetRegexes()

at DeviceDetectorNET.Parser.Client.LibraryParser…ctor()

at DeviceDetectorNET.Parser.Client.ClientType.get_Library()

at DeviceDetectorNET.DeviceDetector.AddClientsParser()

at DeviceDetectorNET.DeviceDetector…ctor(String userAgent)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
cpkuocommented, Jul 12, 2022

So I went into the source code to match up with the stack trace for this exception and realized I was using a dotnet core version package of DeviceDetector.NET that I don’t believe exists anymore. Nuget package manager was not notifying me of updating. So I removed it and started using the latest package of DeviceDetector.NET which now supports the latest .net standard. So far, I have not run into this issue.

1reaction
cpkuocommented, Jun 21, 2022

Sure, there is still the possibility that DeviceDetector will write to it’s internal cache collection on concurrent requests but this seems to have helped me in a high load environment. I wonder if setting a new instance of DictonaryCache on each request in combination with the custom ConcurrentDictionary cache will completely eliminate the error. I will need to review the source code at some point, in the meantime here is some code.

`private static ConcurrentDictionary<string, DeviceInfo> cache = new ConcurrentDictionary<string, DeviceInfo>(); private static DictionaryCache dicationaryCache = new DictionaryCache();

public DeviceInfo Get(string userAgent)
{
    if (cache.ContainsKey(userAgent))
    {
        return cache[userAgent];
    }

    //Domain class
    var info = new DeviceInfo();

    DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE);
    var dd = new DeviceDetector(userAgent);

    dd.SetCache(dicationaryCache);
    dd.Parse();

    var client = dd.GetClient();

    //Transfer result into domain class DeviceInfo
    //  .......

    cache.TryAdd(userAgent, info);

    return info;
}`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Concurrency issues
Concurrency refers to the sharing of resources by multiple interactive users or application programs at the same time. The database manager controls this ......
Read more >
Concurrency (computer science)
In computer science, concurrency is the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or...
Read more >
Concurrency problems in DBMS Transactions
The unrepeatable problem occurs when two or more read operations of the same transaction read different values of the same variable. Example: In ......
Read more >
What is Database Concurrency? - IT Glossary
Dirty read: This issue arises when a particular transaction accesses a data object written or updated by another uncommitted transaction in the database....
Read more >
Solving Common Concurrency Problems
Concurrency is a notorious cause of really frustrating bugs. Most software bugs are consistent. If you do X, then Y, then Z, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found