Skip to content

Cache exception handler lookups instead of re-resolving and contending on every error - #14212

Merged
graemerocher merged 2 commits into
5.2.xfrom
denis/micronaut-exception-handler-contention-81b1d3
Oct 31, 2026
Merged

graemerocher merged 2 commits into
5.2.xfrom
denis/micronaut-exception-handler-contention-81b1d3

Conversation

@dstepanov

Copy link
Copy Markdown
Contributor

Under concurrent error traffic, threads block in DefaultBeanContext.findConcreteCandidate on a ConcurrentHashMap$TreeBin monitor, and JFR shows time in recursive TreeNode.findTreeNode calls under RequestLifecycle.onErrorNoFilter. This happens even when requests keep raising the same exception types.

RequestLifecycle.onErrorNoFilter looks up the handler with a new Qualifiers.byTypeArgumentsClosest(cause.getClass(), Object.class) on every error. Two defects combine:

  • ClosestTypeArgumentQualifier had no equals/hashCode, so the lookup never hit beanConcreteCandidateCache. Every error re-ran the candidate search and inserted a new key, pushing other entries out of the 30-entry cache.
  • BeanCandidateKey.hashCode() was the bean type's hash only. All ExceptionHandler keys landed in one bin, which treeified. The keys are not Comparable, so lookups search the whole tree, and every insert locks that one bin.

Changes:

  • ClosestTypeArgumentQualifier gets equals/hashCode over its type arguments, like TypeArgumentQualifier.
  • BeanCandidateKey hashes the qualifier together with the bean type.

Now each exception type takes one cache entry, and later lookups are lock-free hits. A qualifier that overrides equals without hashCode now misses the cache. Results stay correct; Micronaut's own qualifiers implement both.

BeanConcreteCandidateCacheSpec (repeated lookups keep one entry per exception type; keys with different qualifiers get different hashes) and a new ClosestTypeArgumentSpec case fail without the fix and pass with it.

…uals/hashCode and hash the qualifier into BeanCandidateKey
@dstepanov dstepanov added the type: bug Something isn't working label Oct 30, 2026
@dstepanov
dstepanov marked this pull request as ready for review October 30, 2026 12:28
…e block duplicated from TypeArgumentQualifier
@sonarqubecloud

Copy link
Copy Markdown

@graemerocher
graemerocher merged commit 75ccfed into 5.2.x Oct 31, 2026
18 checks passed
@graemerocher
graemerocher deleted the denis/micronaut-exception-handler-contention-81b1d3 branch October 31, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants