diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 9dbbd3a7..ab63fdc0 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -335,9 +335,11 @@ public final class Gson { } Map, FutureTypeAdapter> threadCalls = calls.get(); + boolean requiresThreadLocalCleanup = false; if (threadCalls == null) { threadCalls = new HashMap, FutureTypeAdapter>(); calls.set(threadCalls); + requiresThreadLocalCleanup = true; } // the key and value type parameters always agree @@ -346,9 +348,10 @@ public final class Gson { return ongoingCall; } - FutureTypeAdapter call = new FutureTypeAdapter(); - threadCalls.put(type, call); try { + FutureTypeAdapter call = new FutureTypeAdapter(); + threadCalls.put(type, call); + for (TypeAdapterFactory factory : factories) { TypeAdapter candidate = factory.create(this, type); if (candidate != null) { @@ -360,6 +363,10 @@ public final class Gson { throw new IllegalArgumentException("GSON cannot handle " + type); } finally { threadCalls.remove(type); + + if (requiresThreadLocalCleanup) { + calls.remove(); + } } }