ClickHouseConnection.CustomSettings writes change the shared client and the caller's settings object

Open
#682 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
databases

Research direction

Start in ClickHouse.Driver/ADO/ClickHouseConnection.cs at the CustomSettings property and the Settings setter, then follow how ClickHouseClient.cs keeps the settings by reference and reads CustomSettings around line 1044. Look at how ChangeDatabase in ClickHouseCommand.cs passes the database per request, as the model for a per-connection setting. Done means a write through one connection leaves the data source, other connections and the caller's ClickHouseClientSettings unchanged, and Clone() copies the connection's settings.

Written by the indexing model from the issue text.

Description

bug
Describe the bug

ClickHouseConnection.CustomSettings returns the live CustomSettings dictionary of the connection's ClickHouseClient (ClickHouseConnection.cs#L161). The client keeps the settings object it gets by reference (ClickHouseClient.cs#L137) and reads that dictionary for each request (ClickHouseClient.cs#L1044).

All connections from one ClickHouseDataSource share one client. A write to CustomSettings on one connection therefore:

  • applies to every other connection of the data source, and to GetClient();
  • changes the ClickHouseClientSettings object that the caller gave to the data source.

The same occurs for connections made with new ClickHouseConnection(client) and with Clone().

There is no way to give one connection its own custom setting. The only alternative is to assign ClickHouseConnection.Settings, which replaces the connection's client with a new client and a new HTTP pool (ClickHouseConnection.cs#L182-L188).

Steps to reproduce
  1. Make a ClickHouseDataSource from a ClickHouseClientSettings object.
  2. Create two connections from it.
  3. Write a custom setting through the first connection.
  4. The second connection and the original settings object both contain the setting.

No server is necessary, because no query runs.

Expected behaviour

A setting written through one connection applies only to that connection. ChangeDatabase already works this way: the connection keeps the database itself and sends it with each request (ClickHouseCommand.cs#L252). The data source, its other connections and the caller's settings object do not change.

Code example
#:package [email protected]
using ClickHouse.Driver.ADO;

var userSettings = new ClickHouseClientSettings("Host=localhost");
using var dataSource = new ClickHouseDataSource(userSettings);
var a = dataSource.CreateConnection();
var b = dataSource.CreateConnection();

a.CustomSettings["join_use_nulls"] = "1";

Console.WriteLine(b.CustomSettings.ContainsKey("join_use_nulls"));            // True
Console.WriteLine(userSettings.CustomSettings.ContainsKey("join_use_nulls")); // True
Suggested fix

Give each connection its own custom settings, and merge them between the client's settings and the command's settings when the command builds its request. The order is client, then connection, then command. Reading connection.CustomSettings should still show the client's settings, so that existing reads do not change. Clone() should copy the connection's database and custom settings.

This changes the result for code that writes connection.CustomSettings to configure every connection of a data source. If that use must continue to work, a new member can hold the per-connection settings, and CustomSettings can become obsolete.

Context

Found while working on ClickHouse.EntityFrameworkCore#50. The EF Core provider must add join_use_nulls to the connections it gets from a user's ClickHouseDataSource. A write to connection.CustomSettings would change the user's data source, so the provider assigns Settings instead, which builds a new client for each DbContext.

Configuration
Environment
  • Client version: 1.5.0
  • Language version: C# 14
  • .NET version: 10.0
  • OS: Linux
ClickHouse server
  • Not needed. The defect occurs without a query.
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.