Skip to content

[ISSUE #7452] skip caching when cache rule handle is null - #7541

Merged
wy471x merged 1 commit into
apache:masterfrom
Emryswu:fix/issue-7452
Oct 11, 2026
Merged

wy471x merged 1 commit into
apache:masterfrom
Emryswu:fix/issue-7452

Conversation

@Emryswu

@Emryswu Emryswu commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #7452

Problem

CachePluginDataHandler.handlerRule() skips a rule whose handle is null, so no CacheRuleHandle is cached for it. When such a rule matches, CachePlugin still wraps the response in CacheHttpResponse with a null handle, and cacheResponse() throws a NullPointerException on cacheRuleHandle.getTimeoutSeconds() while writing an otherwise successful downstream response.

Fix

CachePlugin.doExecute() now looks up the rule handle first. If none is cached, it passes the request to the rest of the chain without reading or writing the cache. This is how the Hystrix, Resilience4J, Sentinel and RateLimiter plugins handle a missing handle since #6877. No default timeout is applied. Rules with a handle behave as before. The handle is now looked up once instead of in both branches.

Tests

Added two tests to CachePluginTest. In both, the downstream chain writes a real response body:

  • testDoExecuteSkipsCacheWhenRuleHandleIsNull: a rule with no cached handle completes without error, returns the downstream body, and writes nothing to the cache. Without the fix, this test fails with the NPE from the issue.
  • testDoExecuteCachesResponseWhenRuleHandleIsPresent: a rule with a handle still returns the body and caches it.

mvn -pl shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler -am test passes locally, checkstyle included.

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

CachePluginDataHandler skips rules whose handle is null, so CachePlugin
could not find a CacheRuleHandle for such a rule and CacheHttpResponse
threw a NullPointerException on getTimeoutSeconds() while writing an
otherwise successful downstream response.

When no rule handle is cached, CachePlugin now passes the request to the
rest of the chain without reading or writing the cache, the same way the
Hystrix, Resilience4J, Sentinel and RateLimiter plugins handle a missing
handle (apache#6877).

Add regression tests for a rule without a handle and for a rule with a
handle.

Fixes apache#7452

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: skip caching when cache rule handle is null

Clean, minimal fix. CachePlugin.doExecute now builds the rule handle once up front and short-circuits to chain.execute(exchange) when the cache rule handle is null, instead of passing a null CacheRuleHandle into CacheHttpResponse (which would NPE / misbehave). It also removes the now-duplicated buildRuleHandle(rule) calls that previously appeared in two branches.

Verification

  • buildRuleHandle returns CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule)), which is null when the rule has no configured handle — so the original code could pass null into CacheHttpResponse. The new null guard is the correct fix.
  • java.util.Objects was already imported (used elsewhere in the file), so the added Objects.isNull(...) compiles.
  • Tests added for both paths: testDoExecuteSkipsCacheWhenRuleHandleIsNull (asserts body flows through unchanged and nothing is written to the cache) and testDoExecuteCachesResponseWhenRuleHandleIsPresent (asserts the response is cached).

CI note
No checks are reported on the fork branch fix/issue-7452 — the fork hasn't triggered CI, so there is no CI evidence for this change. The modification is single-module (shenyu-plugin-cache-handler), self-contained, and well-tested, so risk is low, but please confirm pr_build is green before merging.

Approving — the fix is correct and the added tests cover the regression.

@Aias00

Aias00 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

if you are using wechat, pls add mine: aias00

@wy471x
wy471x merged commit 4e44c60 into apache:master Oct 11, 2026
27 checks passed
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] CachePlugin crashes while writing responses for accepted rules with a null handle

3 participants