Skip to content

[type:fix] guard the tars plugin against an empty proxy list - #7211

Merged
Aias00 merged 7 commits into
apache:masterfrom
HY-love-sleep:fix/tars-empty-proxy-list
Sep 26, 2026
Merged

Aias00 merged 7 commits into
apache:masterfrom
HY-love-sleep:fix/tars-empty-proxy-list

Conversation

@HY-love-sleep

Copy link
Copy Markdown
Contributor

What

TarsPlugin picks one of the proxies of the cached upstream at random:

int index = ThreadLocalRandom.current().nextInt(tarsInvokePrxList.getTarsInvokePrxList().size());

When a path was never initialized the cache loader returns an empty proxy list, so nextInt(0)
throws IllegalArgumentException and every request routed to that path fails with a raw 500
instead of a proper error response. The plugin now answers with
CANNOT_FIND_HEALTHY_UPSTREAM_URL — the same result the divide and websocket plugins already
return for an empty upstream list — and logs the offending path.

Why

Closes #6652.

Tests

  • TarsPluginTest#testTarsPluginWithEmptyProxyList (new): routes a request to a path that was
    never initialized and asserts the plugin completes with the error response instead of throwing.

Two pre-existing tests in the same class asserted the IllegalArgumentException above, i.e. they
could only pass while this bug was present:

  • testTarsPluginWithArgumentTypeMissMatch → now asserts the error response instead of an escaped
    exception.
  • testTarsPluginNormal → now exercises the invocation path and asserts RPC_RESULT is written
    back. Its Method#invoke stub never matched the varargs call, so that part of the test was dead
    code 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
  • checkstyle: 0 violations

HY-love-sleep and others added 5 commits September 24, 2026 13:57
…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 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.

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/metaData checks and before the random index selection, so it triggers exactly on the state that used to blow up.
  • Returning HttpStatus.INTERNAL_SERVER_ERROR with ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL via ShenyuResultWrap.error(...) matches how the sibling RPC plugins report "no usable upstream", so the error shape is consistent across plugins. The LOG.error includes 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:

  • testTarsPluginWithEmptyProxyList is the new regression test: it uses an uninitialized path and asserts the plugin answers 500 instead of throwing.
  • method.invoke(any(), any()) → method.invoke(any(), any(Object[].class)) fixes a real Mockito varargs stubbing bug. Method#invoke is varargs, so any() binds to the first vararg element only and the stub never matched — invoke() returned null and the plugin silently produced no RPC result. Matching the array as a whole is the correct form, and the test now asserts Constants.RPC_RESULT is 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.

@Aias00
Aias00 merged commit 21fa6f3 into apache:master Sep 26, 2026
21 of 39 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] TarsPlugin throws on empty/uninitialized proxy list (nextInt(0))

2 participants