This repository was archived by the owner on May 31, 2022. It is now read-only.
This repository was archived by the owner on May 31, 2022. It is now read-only.
Ensure double-checked locking when loading Jwk definitions #1405
Closed
Description
JwkDefinitionSource.getDefinitionLoadIfNecessary should check if the definition has been loaded in the synchronized before clearing and loading definitions. The current implementation has the side effect of a second thread waiting for the lock will attempt to also reload the definitions.
The following code should prevent this duplicate work.
JwkDefinitionHolder getDefinitionLoadIfNecessary(String keyId) {
JwkDefinitionHolder result = this.getDefinition(keyId);
if (result != null) {
return result;
}
synchronized (this.jwkDefinitions) {
result = this.getDefinition(keyId);
if(result != null) {
return result;
}
this.jwkDefinitions.clear();
for (URL jwkSetUrl : jwkSetUrls) {
this.jwkDefinitions.putAll(loadJwkDefinitions(jwkSetUrl));
}
return this.getDefinition(keyId);
}
}