Skip to content

DataTypeConverter usage has potential thread-safety issues #2728

In review

Description

@janeklb

Description

This is not an explicit bug report in the sense that I haven't experienced a problem (yet?), it is just something I bumped into while browsing the code.

DataTypeConverter has a javadoc comment stating that it is not thread safe. For what it's worth, I have also confirmed thread safety problems because I ended up using the DataTypeConverter.INSTANCE in production (before I realised that it was labelled as non thread safe) which caused some bugs.

After fixing the bugs in my project, I was curious about the fact that a static singleton DataTypeConverter.INSTANCE was even declared in the clickhouse-java repo (why have a singleton instance if it is not thread safe, in an otherwise thread safe library?). Of course this class is in a package named internal, so it's not really my business... :) Still, I did notice that DataTypeConverter.INSTANCE is used in two places which makes me wonder if it could be the source of (other / future / not-yet-encountered) problems..

Activity

  1. chernser commented on Apr 15, 2026

    @chernser
    Contributor

    @janeklb
    Thank you for reporting this!
    We will review the code and make a fix/documentation

  2. added this to the 0.12.0 milestone on Jul 22, 2026
  3. added
    planned_work
    on Nov 18, 2026
  4. chernser commented on Nov 23, 2026

    @chernser
    Contributor

    @polyglotAI-bot please fix it with this instructions:

    3. "How to make this class thread-safe without performance degradation?"
    BaseCollectionConverter is an internal class whose only usages in the repository are ArrayAsStringWriter and ListAsStringWriter inside DataTypeConverter. You can make DataTypeConverter 100% stateless and thread-safe with zero extra allocations (actually fewer allocations and faster than today):
    
    Pass conversion state as method parameters instead of fields (best option):
    Refactor BaseCollectionConverter (or replace the two inner classes with static stateless helpers on DataTypeConverter) so Appendable / StringBuilder and ClickHouseColumn are passed on the stack into convert(...) / onStart(...) / onItem(...) / onEnd(...) instead of stored in this.appendable and this.column.
    Then ArrayAsStringWriter and ListAsStringWriter hold no mutable fields, DataTypeConverter.INSTANCE becomes completely thread-safe with zero locks, zero ThreadLocal lookups, and zero per-call writer allocations, and convertAndReset's try/finally state-swapping disappears completely.
    Bonus zero-cost speedups while keeping output identical:
    Inside onItem, append String elements directly to the existing StringBuilder (or pass StringBuilder into a helper) instead of calling stringToString which allocates a second StringBuilder per element.
    Iterate BinaryStreamReader.ArrayValue.array directly instead of calling asList() when there are no nested ArrayValues (or handle ArrayValue as a sublist in ArrayAsStringWriter).
    Avoid ThreadLocal: Using ThreadLocal<ListAsStringWriter> would fix thread-safety, but it adds ThreadLocal.get() lookup overhead on every call and risks memory retention in pooled or virtual threads for objects that hold no expensive buffers.
    

    The issue is that this class it pure internal and the INSTANCE here is done to avoid all time allocation in readers. There was a plan to use this class to customize converter so make it stateful but thread-safe.

    Make this class thread-safe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugclient-api-v2planned_work

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions