uniVocity / univocity-parsers

uniVocity-parsers is a suite of extremely fast and reliable parsers for Java. It provides a consistent interface for handling different file formats, and a solid framework for the development of new parsers.
918 stars 251 forks source link

Data race in ConcurrentCharLoader.java #487

Open spalac24 opened 3 years ago

spalac24 commented 3 years ago

Hi,

I believe I found a data race in ConcurrentCharLoader.java , which can be triggered by default normal use of the CsvParser library.

Here's a sample minimal code that triggers the issue: https://gist.github.com/spalac24/0256b40b1d6652fdb30e91624f7b5ed0

In short, that code causes stopReading() to be called both from https://github.com/uniVocity/univocity-parsers/blob/master/src/main/java/com/univocity/parsers/common/input/concurrent/ConcurrentCharLoader.java#L101 (which runs in its own thread by default) and https://github.com/uniVocity/univocity-parsers/blob/master/src/main/java/com/univocity/parsers/common/input/concurrent/ConcurrentCharInputReader.java#L85 concurrently, but stopReading() is not thread safe.

This does not reliably cause an exception to be thrown because repeatedly closing a Reader is fine as per the Java spec, but I believe it should still be addressed.