Skip to content

Make JsonStreamParser.next() thread-safe - #3172

Open
Anmol-In-Verse wants to merge 5 commits into
google:mainfrom
Anmol-In-Verse:main
Open

Anmol-In-Verse wants to merge 5 commits into
google:mainfrom
Anmol-In-Verse:main

Conversation

@Anmol-In-Verse

@Anmol-In-Verse Anmol-In-Verse commented Nov 15, 2026 •

Copy link
Copy Markdown

Purpose

JsonStreamParser documents itself as conditionally thread-safe, requiring external synchronization. However, hasNext() already synchronizes on an internal lock while next() does not, making concurrent calls to next() unsafe.

Description

Currently, next() performs a check-then-act sequence: it calls hasNext() (which acquires and releases the internal lock) and then executes Streams.parse(parser) without holding the lock.

When multiple threads call next() concurrently, they can access the same JsonReader simultaneously, potentially causing corrupted parser state, lost or duplicated elements, or unexpected exceptions.

This change:

  • Synchronizes the entire next() operation using the existing internal lock.
  • Ensures the availability check and parsing operation execute atomically.
  • Updates the class Javadoc to reflect the improved thread-safety behavior.
  • Adds a concurrent execution test to verify that multiple threads can safely consume elements.

Testing

  • Added a test for concurrent calls to next().
  • Verified that all elements are consumed without duplication or loss.

Checklist

  • Follows the Google Java Style Guide.
  • No new public API introduced.
  • Unit test added for concurrent execution.
  • mvn clean verify javadoc:jar passes.

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Anmol-In-Verse

Copy link
Copy Markdown
Author

@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.

@Marcono1234 Marcono1234 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the title of this PR (small pencil icon on the right next to the title) because now this PR makes the code fully non-thread-safe?

Otherwise it might be a bit confusing if this PR is merged.

@@ -30,27 +30,16 @@
* asynchronously. The JSON data is parsed in lenient mode, see also {@link

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants