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.

`Session is locked by a concurrent client` from bulk copy

See original GitHub issue

I’m not sure if it is upstream issue in ClickHouse or some races in provider code, so I will fill it here for now.

Following code fails with Session is locked by a concurrent client error for me after ~200 iterations, which is quite annoying as it fails test run at random locations…

CH version: Sessions: enabled

            using var cn = new ClickHouseConnection(cs);
            using var cmd = cn.CreateCommand();
            cmd.CommandText = "drop table if exists test_table";
            cmd.CommandText = "create table test_table(field Int32) Engine=Memory";

            var cnt = 0;
            var values = new object[1][] { new object[1] };
            while (true)
                using var bc = new ClickHouseBulkCopy(cn);
                bc.MaxDegreeOfParallelism = 1;
                bc.DestinationTableName = "test_table";
                values[0][0] = cnt;
                await bc.WriteToServerAsync(values);

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

DarkWanderercommented, Oct 24, 2022

Just to give an update. I’ve figured out the cause for the error - it is because ClickHouse doesn’t like it when calls from same session come from different HTTP connections, even if the queries never come concurrently. Workaround is “simple” - just pass a HttpClient with a single-connection HttpClientHandler:

            new HttpClientHandler()
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                MaxConnectionsPerServer = 1,

Fixing that in scope of the library is a more complex story, however - I’m thinking how to do that in a fashion which is both reasonably backward-compatible and restorative towards this issue

joshbartleycommented, Dec 26, 2022

Recently I looked into the HttpClient and Handler docs and recommendations. The one slightly mentioned, that the Azure client uses, is the SocketsHttpHandler which HttpConnectionHandler uses internally. You wouldn’t rely on the consumer to pass in a HttpClient and/or HttpConnectionHandler as you would have an internal one that handles sockets for you based on the endpoint name. You would need a list of all the connection to pool them, and the SocketsHttpHandler can drop a connection after X time as well to handle dns changes. .net 5 has more exensibility for the Sockets handler as well when 3.1 doesn’t but similar functionality. I’ll look around in the connection code though I don’t know of an easy way to test any style of those changes without a complicated container setup. The Sql Connection pool classes have a lot of history in them that makes me think there are some edge cases that only time will show in any connection pool class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"DB::Exception: Session is locked by a concurrent" error on ...
the problem is that when the number of rows to be copied is like 10 or more times bigger that BatchSize then the...
Read more >
Understand and resolve SQL Server blocking problems
Blocking is an unavoidable and by-design characteristic of any relational database management system (RDBMS) with lock-based concurrency.
Read more >
Conflicting updates in concurrent Business Central sessions
To illustrate locking issues with concurrent data access, I am considering a common solution for bulk data upload when a process imports ...
Read more >
Query hanging in ClientRead and blocking all others
The only way that such a session can block anything is if its state is idle in transaction . All locks are held...
Read more >
Prevent multiple users from running the same SQL Server ...
If @LockOwner is "Session" the lock is held until it is explicitly released with a call to sp_releaseapplock or if the session ends....
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 Post

No results found

github_iconTop Related Hashnode Post

No results found