Repository navigation
[type:fix] guard the tars plugin against an empty proxy list - #7211
Merged
Merged
Conversation
…6652) When the path of a tars request was never initialized, the cache loader returns an empty proxy list and ThreadLocalRandom.nextInt(0) threw an IllegalArgumentException, so the request failed with a raw 500 instead of a proper error response. The plugin now reports CANNOT_FIND_HEALTHY_UPSTREAM_URL, the same result the divide and websocket plugins use for an empty upstream list. Two pre-existing tests in TarsPluginTest asserted that very IllegalArgumentException, so they could only pass while the bug was present; they now assert the error response and the successful invocation path. Their Method#invoke stub never matched the varargs call either - it was dead code until now.
Aias00
approved these changes
Sep 24, 2026
Aias00
left a comment
Contributor
There was a problem hiding this comment.
Summary
TarsPlugin#doExecute called ThreadLocalRandom.current().nextInt(tarsInvokePrxList.getTarsInvokePrxList().size()) without checking the list first. ApplicationConfigCache returns a TarsInvokePrxList with an empty proxy list for a path that was never initialized, so nextInt(0) throws IllegalArgumentException, which surfaces as an unhandled error instead of a proper gateway response.
Review
- The guard is placed after the existing null/
metaDatachecks and before the random index selection, so it triggers exactly on the state that used to blow up. - Returning
HttpStatus.INTERNAL_SERVER_ERRORwithShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URLviaShenyuResultWrap.error(...)matches how the sibling RPC plugins report "no usable upstream", so the error shape is consistent across plugins. TheLOG.errorincludes the offending path, which is what an operator needs to diagnose a mis-registered tars service. CollectionUtils.isEmpty(...)also covers a null list, so both "never initialized" and "all upstreams removed" are handled.
The test changes are a genuine improvement, not just accommodation:
testTarsPluginWithEmptyProxyListis the new regression test: it uses an uninitialized path and asserts the plugin answers500instead of throwing.method.invoke(any(), any())→method.invoke(any(), any(Object[].class))fixes a real Mockito varargs stubbing bug.Method#invokeis varargs, soany()binds to the first vararg element only and the stub never matched —invoke()returnednulland the plugin silently produced no RPC result. Matching the array as a whole is the correct form, and the test now assertsConstants.RPC_RESULTis actually populated, which it could not before.- The two existing tests previously asserted
assertThrows(IllegalArgumentException.class, ...); they were asserting the bug. They now assert the real outcome instead.
CI
All green (pr_build, build, CodeQL, k8s ingress integration suites).
Notes (non-blocking)
Same as #7213: #6309 ("remove tars support") is open, so this may become unnecessary depending on merge order. Not a reason to block.
LGTM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
TarsPluginpicks one of the proxies of the cached upstream at random:When a path was never initialized the cache loader returns an empty proxy list, so
nextInt(0)throws
IllegalArgumentExceptionand every request routed to that path fails with a raw 500instead of a proper error response. The plugin now answers with
CANNOT_FIND_HEALTHY_UPSTREAM_URL— the same result the divide and websocket plugins alreadyreturn for an empty upstream list — and logs the offending path.
Why
Closes #6652.
Tests
TarsPluginTest#testTarsPluginWithEmptyProxyList(new): routes a request to a path that wasnever initialized and asserts the plugin completes with the error response instead of throwing.
Two pre-existing tests in the same class asserted the
IllegalArgumentExceptionabove, i.e. theycould only pass while this bug was present:
testTarsPluginWithArgumentTypeMissMatch→ now asserts the error response instead of an escapedexception.
testTarsPluginNormal→ now exercises the invocation path and assertsRPC_RESULTis writtenback. Its
Method#invokestub never matched the varargs call, so that part of the test was deadcode until the plugin actually reached it.
Verified
./mvnw -pl shenyu-plugin-tars -am test -Dtest='TarsPluginTest,TarsPluginDataHandlerTest,TarsMetaDataHandlerTest,ApplicationConfigCacheTest,PrxInfoUtilTest'→ Tests run: 24, Failures: 0, Errors: 0