Repository navigation
Make JsonStreamParser.next() thread-safe - #3172
Anmol-In-Verse wants to merge 5 commits into
Conversation
eamonnmcmanus
left a comment
There was a problem hiding this comment.
I think the presence of the lock field in this class is a historical accident, dating from when JSON parsing was done with a JavaCC-generated parser (back in about 2009). In my opinion there is no real benefit to trying to make this class thread-safe, so I would be in favour of removing the lock field and the synchronization, and updating the Javadoc to say that the class is not thread-safe and concurrent accesses to it must be externally synchronized. You're certainly going to want to synchronize around code that does hasNext() + next(), and this class can't do that on its own.
Feel free to update the PR in that direction.
|
@eamonnmcmanus Thanks for the review! I've updated the PR as suggested: removed the lock field and internal synchronization, and documented that the class is not thread-safe and concurrent accesses must be externally synchronized. The concurrency test has been removed as well. |
| @@ -30,27 +30,16 @@ | |||
| * asynchronously. The JSON data is parsed in lenient mode, see also {@link | |||
There was a problem hiding this comment.
Should this "asynchronously" here be removed as well? I am not sure exactly what it means, or how this class is "more async" than a regular JsonReader.
(This is only a suggestion; I am not a direct project member)
Purpose
JsonStreamParserdocuments itself as conditionally thread-safe, requiring external synchronization. However,hasNext()already synchronizes on an internal lock whilenext()does not, making concurrent calls tonext()unsafe.Description
Currently,
next()performs a check-then-act sequence: it callshasNext()(which acquires and releases the internal lock) and then executesStreams.parse(parser)without holding the lock.When multiple threads call
next()concurrently, they can access the sameJsonReadersimultaneously, potentially causing corrupted parser state, lost or duplicated elements, or unexpected exceptions.This change:
next()operation using the existing internal lock.Testing
next().Checklist
mvn clean verify javadoc:jarpasses.