UseSession: concurrent calls of one session fail with SESSION_IS_LOCKED, but the docs say the client serializes them
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by comparing the session handling described in docs/http.mdx and the send path in ClickHouse.Driver/ClickHouseClient.cs, then inspect ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs and the removed Session_ConcurrentRequests_AreSerialized test mentioned in the issue. A maintainer must decide whether calls should be serialized or the docs should change; done means implementing that decision and covering the concurrent session behavior with a regression test.
Written by the indexing model from the issue text.
Description
Describe the bug
docs/http.mdx says that the client serializes the requests of a session: "Enable stateful sessions; serializes requests" (Session management table, UseSession), and "When sessions are enabled, requests are serialized to prevent concurrent use of the same session." (Best practices, Sessions). The client does not do this.
With UseSession=true, when two calls of one ClickHouseClient, or of one ClickHouseConnection, run at the same time, the client sends both requests at once. The second call does not wait: it fails at once with SESSION_IS_LOCKED (373). Version 0.9.0 waits, and the second call returns its result.
Steps to reproduce
- Run the program below against a server on
clickhouse:8123. - For each API, it starts
SELECT sleep(2), and 300 ms laterSELECT 1, on one session.
Expected behaviour
As the docs say: SELECT 1 waits for SELECT sleep(2) and returns 1. This is the output with [email protected] (ADO.NET case; 0.9.0 has no ClickHouseClient):
SELECT 1 -> 1 at 2056 ms
SELECT sleep(2) -> 0 at 2056 ms
If the client must not serialize the calls, the docs must tell the caller not to run two calls of one session at the same time.
Code example
Reproduction program (C#, 27 lines)
#:package [email protected]
using System.Diagnostics;
using ClickHouse.Driver;
using ClickHouse.Driver.ADO;
const string cs = "Host=clickhouse;Port=8123;UseSession=true";
async Task Run(string api, Func<string, Task<object>> exec)
{
var sw = Stopwatch.StartNew();
async Task Report(string sql, int delayMs)
{
await Task.Delay(delayMs);
try { Console.WriteLine($"{api}: {sql} -> {await exec(sql)} at {sw.ElapsedMilliseconds} ms"); }
catch (Exception e) { Console.WriteLine($"{api}: {sql} -> {e.Message.Split('\n')[0]} at {sw.ElapsedMilliseconds} ms"); }
}
await Task.WhenAll(Report("SELECT sleep(2)", 0), Report("SELECT 1", 300));
}
using (var client = new ClickHouseClient(cs))
await Run("ClickHouseClient", sql => client.ExecuteScalarAsync(sql));
using (var conn = new ClickHouseConnection(cs))
{
await conn.OpenAsync();
await Run("ClickHouseConnection", sql => { var cmd = conn.CreateCommand(); cmd.CommandText = sql; return cmd.ExecuteScalarAsync(); });
}
Error log
ClickHouseClient: SELECT 1 -> Code: 373. DB::Exception: Session f2fb0348-b301-4465-937b-c94851c4bb3c is locked by a concurrent client. (SESSION_IS_LOCKED) at 324 ms
ClickHouseClient: SELECT sleep(2) -> 0 at 2068 ms
ClickHouseConnection: SELECT 1 -> Code: 373. DB::Exception: Session 2e210f36-f159-4967-8eb5-b5aa54e64eac is locked by a concurrent client. (SESSION_IS_LOCKED) at 307 ms
ClickHouseConnection: SELECT sleep(2) -> 0 at 2008 ms
The same cases against main give the same result. Without UseSession, both calls of ClickHouseClient succeed.
Root cause
#219 (first release 1.0.0) removed the session SemaphoreSlim and the single-connection handler from the send path, and it removed the test Session_ConcurrentRequests_AreSerialized. Now CreateHttpClientFactory always makes a DefaultPoolHttpClientFactory (ClickHouse.Driver/ClickHouseClient.cs:1386), and SendAsync sends each request at once (ClickHouse.Driver/ClickHouseClient.cs:1404). ClickHouse.Driver/Http/SingleConnectionHttpClientFactory.cs has no caller. The docs (docs/http.mdx:120, docs/http.mdx:1591) still describe the old behavior.
The fix needs a maintainer decision: serialize the requests of a session again, or change the docs to say that the caller must serialize them. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: #784 (a cancelled query keeps the session locked) is a different cause with the same error.
Configuration
Environment
- Client version: 1.5.0 (NuGet), and
mainat 63dc6d3b; 0.9.0 (NuGet) for the expected output - Language version: C#, default of the .NET SDK 11.0.100
- .NET version: .NET 11.0.0 (the program); .NET 10.0.15 for the same cases against
main - OS: Ubuntu 26.04 (Docker)
ClickHouse server
- ClickHouse Server version: 26.11.4.11
- ClickHouse Server non-default settings, if any: none
Classification: impact: none · area: Queries (sessions, UseSession)
Found by automated analysis (Polyglot AI) while working on #784. Verified against a live server.
- Dominant language
- C#
- Stars
- 102
- Forks
- 31
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 40
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/clickhouse-cs
-
bug documentation queries
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#815 ·
Maintainers usually reply within 1 day
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-cs#813 ·
Maintainers usually reply within 1 day
-
bug data-types documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#811 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#809 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#806 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-cs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xmlPossibly taken @alexeyfala claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
danipen/TextMateSharp#145 · 2 comments ·
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Klyroo/klyroo-dashboard#12 ·
Maintainers usually reply within 1 day
-
CONTRIBUTING rules Node out in step 0, then tells client-side contributors to run node --test, which is what CI gates onPossibly taken @yadavraj2001 claimed this today. Opendocumentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
juandresrodca/AdminForge#73 · 1 comment ·
Maintainers usually reply within 1 day
-
[辞書] VIVANTOpen要トリアージ 辞書
Difficulty 1/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day