Skip to content

Commit

Permalink
Use the same lock for all methods accessing Validation.GetValidationP…
Browse files Browse the repository at this point in the history
…roviderListAction#providersPerClassloader

`clearCache` was a static method and thus was synchronized on the class,
while `getCachedValidationProviders`/`cacheValidationProviders`
are instance methods and thus are synchronized on a particular instance.
This means it was theoretically possible for `clearCache` and
`getCachedValidationProviders`/`cacheValidationProviders` to access the
providersPerClassloader map concurrently,
which is a problem because WeakHashMap is not thread-safe.

This may fix #180, though it's hard to say without a proper thread dump
showing what the cause of the deadlock was exactly.

In any case, this will prevent synchronization issues for integrators
that call Validation#clearDefaultValidationProviderResolverCache
concurrently to Validation.DefaultValidationProviderResolver#getValidationProviders.
  • Loading branch information
yrodiere committed Jul 20, 2022
1 parent 33481c4 commit 1aac339
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/jakarta/validation/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ ProviderSpecificBootstrap<T> byProvider(Class<U> providerType) {
*/
@SuppressWarnings("unused")
private static void clearDefaultValidationProviderResolverCache() {
GetValidationProviderListAction.clearCache();
GetValidationProviderListAction.INSTANCE.clearCache();
}

//private class, not exposed
Expand Down Expand Up @@ -339,8 +339,8 @@ public static synchronized List<ValidationProvider<?>> getValidationProviderList
}
}

public static synchronized void clearCache() {
INSTANCE.providersPerClassloader.clear();
public synchronized void clearCache() {
providersPerClassloader.clear();
}

@Override
Expand Down

0 comments on commit 1aac339

Please sign in to comment.