You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DataTypeConverter usage has potential thread-safety issues #2728
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 twoplaces which makes me wonder if it could be the source of (other / future / not-yet-encountered) problems..
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.
Activity
chernser commentedon Apr 15, 2026
@janeklb
Thank you for reporting this!
We will review the code and make a fix/documentation
chernser commentedon Nov 23, 2026
@polyglotAI-bot please fix it with this instructions:
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