UseSession: concurrent calls of one session fail with SESSION_IS_LOCKED, but the docs say the client serializes them

Open
#785 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
backend, databases

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

bug documentation queries
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
  1. Run the program below against a server on clickhouse:8123.
  2. For each API, it starts SELECT sleep(2), and 300 ms later SELECT 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 main at 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/clickhouse-cs

All issues in ClickHouse/clickhouse-cs

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.