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

ArrayIndexOutOfBoundsException when calling json.dump concurrently with date/time #77

Open
robbavey opened this issue Sep 23, 2019 · 1 comment

Comments

@robbavey
Copy link
Contributor

Running json.dump concurrently across multiple threads can cause an ArrayIndexOutOfBoundsException:

warning: thread "Ruby-0-Thread-53: jrjack.rb:1" terminated with exception (report_on_exception is true):
java.lang.ArrayIndexOutOfBoundsException: 965
	at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
	at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2397)
	at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2312)
	at java.util.Calendar.setTimeInMillis(Calendar.java:1804)
	at java.util.Calendar.setTime(Calendar.java:1770)
	at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
	at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
	at java.text.DateFormat.format(DateFormat.java:345)
	at com.jrjackson.RubyAnySerializer.serializeTime(RubyAnySerializer.java:244)
	at com.jrjackson.RubyAnySerializer.serialize(RubyAnySerializer.java:196)
	at com.jrjackson.RubyAnySerializer.serializeHash(RubyAnySerializer.java:226)
	at com.jrjackson.RubyAnySerializer.serialize(RubyAnySerializer.java:162)
	at com.jrjackson.JrJacksonBase.generate(JrJacksonBase.java:73)

Snipped of code that reproduces the issue (courtesy of @jsvd)

require 'jrjackson'                                                                                                         

Thread.abort_on_exception = true

now = Time.now

# we need multi threading to trigger this issue
num_threads = 100 

threads = num_threads.times.map do |i| 
  Thread.new do
    # this is OK: 
    #   loop { JrJackson::Json.dump(now) }
    # but this causes exception:
    loop { JrJackson::Json.dump("a" => now) }
  end 
end
threads.each(&:join)
@jsvd
Copy link
Collaborator

jsvd commented Sep 23, 2019

This is because simpledateformat isn't thread safe and DateTimeFormatter should be used instead (read more here)

"Offending" code is here https://github.com/guyboertje/jrjackson/blob/master/src/main/java/com/jrjackson/RubyAnySerializer.java#L233-L245

robbavey added a commit to robbavey/jrjackson that referenced this issue Sep 23, 2019
Fixes ArrayIndexOutOfBoundsException that can occur when serializing dates concurrently.
Also cache the value of "UTC" TimeZone locally to avoid contention to synchronized
`getTimeZone` method.

Fixes guyboertje#76, guyboertje#77
colinsurprenant pushed a commit that referenced this issue Sep 25, 2019
* Fix concurrency issues

Fixes ArrayIndexOutOfBoundsException that can occur when serializing dates concurrently.
Also cache the value of "UTC" TimeZone locally to avoid contention to synchronized
`getTimeZone` method.

Fixes #76, #77

* Use a ThreadLocal to hold SimpleDateFormat

Avoid unnecessary (and expensive) SimpleDateFormat object creation
by storing in a ThreadLocal.

* Improve changelog message

* Fix typo in variable name
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

No branches or pull requests

2 participants