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.

Hang on calling database from Lazy<T> initialization by multiple threads

See original GitHub issue

Looks closely related to the #1043

Describe the bug I’ve migrated two projects from SDK v2 to V3. After migration I noticed that tests are dramatically slower and they hang randomly. They all pass only when parallel execution is disabled.

To Reproduce I’ve submitted repro to the https://github.com/lukasz-pyrzyk/CosmosDbHangWithParallelTests. It has also branch with SDK V2, which works correctly.

Worth to mention CheckIfDatabaseExists looks like a root cause of the issue. It does an async database call and it’s called from the Lazy<T>. We usually use this concept when we want to initialize a single instance across the app and run some provisioning, like upsert of the stored procedures.

        private async Task<Database> CheckIfDatabaseExists(Settings settings)
        {
            var database = _cosmosClient.GetDatabase(settings.DatabaseId);
            var read = await database.ReadStreamAsync().ConfigureAwait(false);
            if (read.StatusCode == HttpStatusCode.NotFound)
            {
                throw new Exception($"CosmosDB database {settings.DatabaseId} does not exist!");
            }

            return database;
        }

During the investigation, I have replaced the async call to the database inside CheckIfDatabaseExists with await Task.Delay(100).ConfigureAwait(false);

        private async Task<Database> CheckIfDatabaseExists(Settings settings)
        {
            var database = _cosmosClient.GetDatabase(settings.DatabaseId);
            await Task.Delay(100).ConfigureAwait(false);
            return database;
        }

with the following change, tests finish successfully, even in parallel.

Environment summary SDK Version: 3.4.1 OS Version: Windows 10

cc @ealsur @j82w

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
lukasz-pyrzykcommented, Dec 16, 2019

OK, I have the issue.

I noticed that ConfigureAwait was missing in the call method calling ReadStreamAsync. Because of that, continuation wasn’t able to start inside lazy<t> when more threads were not available.

I have double-checked and I’m happy to say that issue was occurring on SDK 3.4.1, but it’s fixed in 3.5.1.

Thanks for help

1reaction
j82wcommented, Dec 4, 2019

This seems very similar to #1043 . @ealsur is working on a fix that will hopefully fix this issue too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LazyInitializationException when using threads - java
1 Answer. The Hibernate session and the entities loaded from that session are not thread safe. If you try accessing them from different...
Read more >
Is lazy loading always required?
I know lazy loading is required when an expensive database call is being made. It would make sense using lazy loading in this...
Read more >
Lazy initialization and double-checked locking with atomics
If not, then all threads calling concurrently except one will wait until the remaining thread has completed the initialization.
Read more >
Lazy initialisation in C++ and Multi-threading
We want a lazy call to the database, so each object at the start knows only its ID and then connects to the...
Read more >
Lazy and once-only C# async initialization
These are conceptually pretty straightforward, although the details can get messy once multiple threads are involved.
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