Skip to content

fix: invoke RedisRateLimiter error callback/logging inside onErrorResume (#6645) - #7029

Merged
Aias00 merged 7 commits into
masterfrom
fix/#6645
Sep 4, 2026
Merged

Aias00 merged 7 commits into
masterfrom
fix/#6645

Conversation

@dmsolr

@dmsolr dmsolr commented Sep 3, 2026

Copy link
Copy Markdown
Member

Fixes #6645

Root cause

In RedisRateLimiter.isAllowed, onErrorResume absorbed every Redis error into a normal Flux.just(1L, -1L) emission, so the trailing .doOnError(...) (which logged the error and invoked the rate limiter algorithm's callback, e.g. cleanup of optimistically-written keys in ConcurrentRateLimiterAlgorithm) was unreachable dead code. On a Redis outage, every request was silently fail-opened with no log signal and no cleanup callback.

Fix

Move the algorithm callback invocation and error logging into the onErrorResume lambda itself, so they actually execute when a Redis error occurs. Removed the now-dead .doOnError(...).

Testing

  • Added allowedThrowableInvokesAlgorithmCallbackTest in RedisRateLimiterTest, using the concurrent algorithm, asserting the algorithm's error callback (Redis zset cleanup) is invoked when the Redis call errors. This test fails against the old code and passes after the fix.

  • ./mvnw -pl shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter -am test: checkstyle 0 violations, full reactor build success, all tests pass (including 8/8 in RedisRateLimiterTest).

  • 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 (module-scoped).

…ume (#6645)

onErrorResume absorbed every Redis error into a normal Flux.just(1L, -1L)
emission, so the trailing doOnError (which logged the error and invoked
the rate limiter algorithm's callback, e.g. cleanup of optimistically
written keys) was unreachable dead code. On a Redis outage every request
was silently fail-opened with no log signal and no cleanup callback.

Move the callback invocation and error logging into the onErrorResume
lambda itself so they actually execute when Redis errors occur.
@dmsolr
dmsolr requested review from Aias00 and a lite review from Copilot September 3, 2026 06:18

Copilot AI 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.

🟡 Changes recommended

The new error-path logging drops the Throwable stack trace and the callback invocation isn’t guarded, which can undermine debuggability and potentially break the intended fail-open behavior if the callback throws.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes a bug in the rate limiter’s Redis error handling where Redis failures were being absorbed by onErrorResume, making downstream .doOnError(...) logging and algorithm cleanup callbacks unreachable (silent fail-open with no cleanup signal).

Changes:

  • Moves rate-limiter algorithm callback invocation and error logging into the onErrorResume error handler in RedisRateLimiter.isAllowed.
  • Removes the now-dead downstream .doOnError(...) block.
  • Adds a unit test asserting the concurrent algorithm’s error callback (Redis ZSET cleanup) is invoked when the Redis script execution errors.
File summaries
File Description
shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiter.java Ensures algorithm callback + logging occur on Redis errors by executing them inside onErrorResume, fixing previously unreachable error side-effects.
shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiterTest.java Adds a regression test confirming the concurrent algorithm cleanup callback is invoked on Redis execution failure.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@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.

Thanks @dmsolr!

The root-cause analysis is correct: onErrorResume absorbs the Redis error before it can reach the trailing .doOnError(...), so the algorithm callback and error logging were unreachable dead code — on a Redis outage every request silently fail-opened.

The fix is sound:

  • Moving rateLimiterAlgorithm.callback(script, keys, scriptArgs) and the error log into the onErrorResume lambda makes both actually execute; the argument is equivalent to the old rateLimiterAlgorithm.getScript().
  • Wrapping the callback in try/catch is a good defensive addition — a callback failure (e.g. Redis still down during zset cleanup) can no longer break the fallback emission.
  • Fail-open semantics (allowed=true, tokensRemaining=-1) are unchanged, so this is purely a correctness/observability fix with no behavioral regression.
  • The new test allowedThrowableInvokesAlgorithmCallbackTest fails on the old code and passes on the new one, and its mocks match ConcurrentRateLimiterAlgorithm#callback (which calls opsForZSet().remove(...).subscribe() — the mock interaction is recorded synchronously, so verify is reliable).

LGTM, approving.

@Aias00
Aias00 merged commit 795cec9 into master Sep 4, 2026
40 checks passed
eye-gu pushed a commit to eye-gu/shenyu that referenced this pull request Sep 30, 2026
…ume (apache#6645) (apache#7029)

* fix: invoke RedisRateLimiter error callback/logging inside onErrorResume (apache#6645)

onErrorResume absorbed every Redis error into a normal Flux.just(1L, -1L)
emission, so the trailing doOnError (which logged the error and invoked
the rate limiter algorithm's callback, e.g. cleanup of optimistically
written keys) was unreachable dead code. On a Redis outage every request
was silently fail-opened with no log signal and no cleanup callback.

Move the callback invocation and error logging into the onErrorResume
lambda itself so they actually execute when Redis errors occur.

* Improve error handling in RedisRateLimiter

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

---------

Co-authored-by: Luke Haochao Zhuang <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: aias00 <[email protected]>
@yuluo-yx
yuluo-yx deleted the fix/#6645 branch November 4, 2026 15:14
@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] RedisRateLimiter silently swallows Redis errors and fail-opens (dead doOnError)

3 participants