tototoshi/scala-csv

CSVWriter is not thread-safe

yonkeltron opened this issue · 2 comments

I just tried using an instance of CSVWriter from inside a few different Akka Future instances and found that writers are not thread-safe.

Please examine the following snippet from a generated CSV file:

ax,dx,cx,ax,ax,bx,dx,bx,cx,cx
cx,bx,cx,dx,bx,dx,ax,ax,dx,cxcx,bx,cx,bx,ax,dx,ax,cx,dx,bx

cx,dx,ax,dx,ax,ax,bx,bx,cx,dx
cx,dx,ax,bx,cx,ax,bx,cx,dx,dxbx,bx,cx,dx,dx,ax,dx,cx,cx,ax
bx,cx,bx,cx,ax,dx,dx,bx,ax,ax
ax,bx,cx,dx,ex,fx,gx,hx,ix,jx

cx,ax,dx,bx,cx,ax,bx,dx,cx,ax
cx,dx,ax,bx,ax,cx,bx,dx,dx,cx

This realization means that this library cannot be used in concurrent applications.

Environment info:

Scala version: 2.11.8

Running Mac OSX El Capitan 10.11.4 (15E65)

$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

$ uname -a
Darwin Concurrent-Chickpea.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

$ sbt version
...snip...
[info] 1.0

Please let me know if I can provide any additional information.

The CSV Writer shouldn't be thread safe, you should handle the thread-safety your self, A simple way to do that is to wrap the CSV Writer with synchornized blocks over the write and close methods 👍

Yes. CSVWriter is not thread-safe but it doesn't mean that it cannot be used in concurrent application.