We're updating the issue view to help you get more done. 

Make clojure.data.csv/write-csv thread-safe

Description

If clojure.data.csv/write-csv is called from multiple threads, the data gets garbled in the file, even if though we maybe using a synchronized writer. The following test will fail on the current master branch:

1 2 3 4 5 6 7 (deftest write-from-multiple-threads (let [string-writer (StringWriter.) futures (mapv #(future (write-csv string-writer [[% %]])) (range 10))] (mapv deref futures) (is (= (set (read-csv (.toString string-writer))) (set (map (juxt str str) (range 10)))))))

The problem here is because .write method on the writer is called multiple times for writing a single line to the CSV file. A potential solution for this would be to build a string for a line in the CSV file, and then call .write on the file writer only once. Attached is a patch that does this using a StringWriter in clojure.data.csv/write-csv*.

Environment

None

Status

Assignee

Unassigned

Reporter

Mourjo Sen

Labels

None

Approval

None

Patch

Code and Test

Priority

Major