-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Description
At Google we import Gson from HEAD periodically. After the last such import, we had reports of a server appearing to get stuck, which we were able to track down to a class loading deadlock introduced by #2727. In short, what happens is that Thread 1 calls GsonBuilder.registerTypeHierarchyAdapter
which triggers a load of the TypeAdapters
class here. That in turn requires JsonElementTypeAdapter
to be loaded for a static final field here. Meanwhile, Thread 2 calls GsonBuilder.create
, which triggers a load of JsonElementTypeAdapter
here. That involves calling TypeAdapters.newTypeHierarchyFactory
here, which requires TypeAdapters
to be loaded. So Thread 1 is loading TypeAdapters
and waiting for JsonElementTypeAdapter
to be loaded, while Thread 2 is loading JsonElementTypeAdapter
and waiting for TypeAdapters
to be loaded.
The deadlock might be due to my request on that PR that the static final fields in TypeAdapters
be preserved, because some existing projects are depending on them.
The fix is straightforward, in any case. Instead of JsonElementTypeAdapter
and EnumTypeAdapter
being public classes that are referenced directly from the Gson
class, we should go back to having that class reference static final fields in TypeAdapters
. Then every classloading path goes through TypeAdapters
and the deadlock is averted.
Fortunately this bug didn't make it into any release, so most users will be unaffected.