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

Gson taking a lock on SimpleDateFormat #1726

Open
ferozed opened this issue Jun 22, 2020 · 4 comments
Open

Gson taking a lock on SimpleDateFormat #1726

ferozed opened this issue Jun 22, 2020 · 4 comments

Comments

@ferozed
Copy link

ferozed commented Jun 22, 2020

While debugging a problem in our service, I noticed that Gson is using SimpleDateFormat. While doing that, it is taking a lock that causes other threads using that same instance of Gson to block...

2020-06-22_19:00:26.64218    java.lang.Thread.State: BLOCKED (on object monitor)
2020-06-22_19:00:26.64218       at com.google.gson.DefaultDateTypeAdapter.serialize(DefaultDateTypeAdapter.java:71)
2020-06-22_19:00:26.64218       - waiting to lock <0x00000000d016dc68> (a java.text.SimpleDateFormat)
2020-06-22_19:00:26.64219       at com.google.gson.DefaultDateTypeAdapter.serialize(DefaultDateTypeAdapter.java:37)
2020-06-22_19:00:26.64219       at com.google.gson.internal.bind.TreeTypeAdapter.write(TreeTypeAdapter.java:81)
2020-06-22_19:00:26.64220       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
2020-06-22_19:00:26.64220       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
2020-06-22_19:00:26.64220       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
2020-06-22_19:00:26.64221       at com.google.gson.Gson.toJson(Gson.java:669)
2020-06-22_19:00:26.64221       at com.google.gson.Gson.toJson(Gson.java:648)
2020-06-22_19:00:26.64221       at com.google.gson.Gson.toJson(Gson.java:603)
2020-06-22_19:00:26.64221       at com.google.gson.Gson.toJson(Gson.java:583)

41 threads waiting on that lock....

$ grep -c 'waiting to lock <0x00000000d016dc68>' /opt/app/log/current
41

This is with gson-2.8.0

@lyubomyr-shaydariv
Copy link
Contributor

@sblommers
Copy link

sblommers commented Jan 26, 2024

We are having the same problem, especcially with heavy load. It simply blocked with a while loop and a concurrent unit test so this surely deserves some attention.

We implemented a custom DateTypeAdapter using FastDateFormat and the troubles went away.

#162

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Jan 26, 2024

surely deserves some attention

The underlying issue is that Gson currently uses java.text.SimpleDateFormat for the default format, and instances of that class are not thread-safe. Maybe there are ways to improve this though, such as using ThreadLocal (might have its drawbacks with virtual threads?).

However, in general it would be good if you registered a custom TypeAdapter for java.util.Date if possible, because the date format used by the default adapter is problematic, see #2472. In that custom adapter you can then also use the java.time.format.DateTimeFormatter class (converting to Instant temporarily) if you want.

@fireworkit
Copy link

meet this error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants