Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

En-threadsafe json injections for collections #224

Merged
merged 1 commit into from
Jul 20, 2015

Conversation

bkirwi
Copy link
Contributor

@bkirwi bkirwi commented Jul 20, 2015

Hi!

Currently, the injections produced by the JsonInjection implicits are not threadsafe, since they reuse the same builder instance. If two different threads call into invert at the same time, both threads will clear and then add items to that builder, and the results of this are undefined.

This patch switches to using a brand-new builder instance for every call to invert. The resulting injections should be safe to use from multiple threads.

This involves a small change to the API: in particular, it drops the fromBuilder method and leans everything on collectionJson. Seemed mostly harmless, since the fromCollection method implements roughly the same functionality in a safer way -- but it is a breaking change.

The existing fromBuilder method reused the same builder for multiple `invert`
calls, which is not safe under multithreading: one thread might, for example,
clear the builder while another thread is adding elements.

This patch changes the implementation to get a new builder every time from the
CanBuildFrom, and puts that behind the existing fromCollection interface. The
existing injections for specific collections should not change behaviour.
@johnynek
Copy link
Collaborator

Thanks for catching this!

johnynek added a commit that referenced this pull request Jul 20, 2015
En-threadsafe json injections for collections
@johnynek johnynek merged commit 32f24a9 into twitter:develop Jul 20, 2015
@bkirwi
Copy link
Contributor Author

bkirwi commented Jul 20, 2015

NP! Thanks for the pull.

@bkirwi bkirwi deleted the threadsafe-json-injections branch July 21, 2015 00:34
@bkirwi bkirwi mentioned this pull request Jan 19, 2016
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.

None yet

3 participants