[GH-ISSUE #5960] [Bug]: Balancer loopback routing rules placed incorrectly in routing table causes infinite loop #4133

Closed
opened 2026-10-31 20:37:25 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @pulsarice on GitHub (Jul 14, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5960

Describe the bug

When a balancer fallback chain is configured (e.g., Balancer1 → Balancer2 → Balancer3), the routing rules for loopback outbounds that handle the fallback are automatically placed at the end of the routing table. This causes a routing loop: when all selectors of the parent balancer fail and fallback to the loopback, the request re-enters the routing table and matches the parent balancer rule again instead of the loopback rule, creating an infinite loop that crashes the Xray kernel.

How to reproduce the problem

  1. Open the panel and create two balancers (Balancer1 and Balancer2) with selectors
  2. Configure Balancer1 to use Balancer2 as a fallback by setting fallbackTag to "_bl_Balancer2"
  3. Create a routing rule that directs traffic by domain to Balancer1
  4. Save and observe the generated Xray config
  5. Make a request to the configured domain when all selectors of Balancer1 are unavailable
  6. Observe that the Xray cpu usage spikes due to routing loop

Alternatively, check the generated config: the loopback routing rule (inboundTag: "_bl_Balancer2" → Balancer2) appears after the domain rule (domain: "example.com" → Balancer1), causing the issue.

Expected behavior

When a request fails all selectors in Balancer1, it should fallback to the loopback outbound (_bl_Balancer2). The loopback routing rule should be placed BEFORE any domain/IP rules that reference the parent balancer, so the loopback rule matches first and correctly routes to Balancer2 without re-triggering the parent balancer rule.

Actual behavior

The loopback routing rule for the fallback balancer is placed at the end of the routing table, after the domain/IP rules. When a request enters the loopback and re-enters the routing table, it matches the parent balancer rule again instead of the loopback rule, creating an infinite loop that crashes the Xray kernel.

Relevant logs


Screenshots

No response

3x-ui version

3.5.0

Xray-core version

26.7.11

How did you install 3x-ui?

install.sh script, Docker / Docker Compose, Manual build from source, Other (please describe in the bug body)

Operating system

Debian 13

Which parts of the panel are affected?

Xray config generation

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

No response

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @pulsarice on GitHub (Jul 14, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5960 ### Describe the bug When a balancer fallback chain is configured (e.g., Balancer1 → Balancer2 → Balancer3), the routing rules for loopback outbounds that handle the fallback are automatically placed at the end of the routing table. This causes a routing loop: when all selectors of the parent balancer fail and fallback to the loopback, the request re-enters the routing table and matches the parent balancer rule again instead of the loopback rule, creating an infinite loop that crashes the Xray kernel. ### How to reproduce the problem 1. Open the panel and create two balancers (Balancer1 and Balancer2) with selectors 2. Configure Balancer1 to use Balancer2 as a fallback by setting fallbackTag to "_bl_Balancer2" 3. Create a routing rule that directs traffic by domain to Balancer1 4. Save and observe the generated Xray config 5. Make a request to the configured domain when all selectors of Balancer1 are unavailable 6. Observe that the Xray cpu usage spikes due to routing loop Alternatively, check the generated config: the loopback routing rule (inboundTag: "_bl_Balancer2" → Balancer2) appears after the domain rule (domain: "example.com" → Balancer1), causing the issue. ### Expected behavior When a request fails all selectors in Balancer1, it should fallback to the loopback outbound (_bl_Balancer2). The loopback routing rule should be placed BEFORE any domain/IP rules that reference the parent balancer, so the loopback rule matches first and correctly routes to Balancer2 without re-triggering the parent balancer rule. ### Actual behavior The loopback routing rule for the fallback balancer is placed at the end of the routing table, after the domain/IP rules. When a request enters the loopback and re-enters the routing table, it matches the parent balancer rule again instead of the loopback rule, creating an infinite loop that crashes the Xray kernel. ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.5.0 ### Xray-core version 26.7.11 ### How did you install 3x-ui? install.sh script, Docker / Docker Compose, Manual build from source, Other (please describe in the bug body) ### Operating system Debian 13 ### Which parts of the panel are affected? Xray config generation ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? _No response_ ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:37:25 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 14, 2026):

Investigated against the source; this is a real bug in this repo's frontend config-generation code, not Xray-core itself.

Title: ensureBalancerLoopback appends the fallback loopback rule to the end of routing.rules, so it can land after a rule that already routes to the parent balancer

Severity: High

Category: Correctness

Why this matters:
ensureBalancerLoopback (frontend/src/pages/xray/balancers/balancer-loopback.ts:55-87) builds a balancer's fallback loopback rule and, when no rule for that inboundTag exists yet, appends it with Array.prototype.push (lines 81-85) unconditionally at the end of settings.routing.rules. It's invoked from BalancersTab.tsx:215,238 whenever a balancer's fallbackTag is set to another balancer, and from XrayPage.tsx:196 (ensureMissingBalancerLoopbacks) on every "Save All", which backfills any loopback rule that's missing. General rules added through the Routing tab are likewise appended (RoutingTab.tsx:209), so the order between a loopback rule and a general rule targeting the same balancer is purely a function of click/edit chronology — nothing enforces that the loopback rule comes first.

Xray-core evaluates routing.rules top-to-bottom, first match wins; this repo's own docs say so explicitly (docs/content/docs/en/operations/outbounds-routing.mdx:115: "Rules are evaluated top-to-bottom — the first match wins"), which is also why the Routing tab offers manual drag-to-reorder. On the Go side, internal/xray/config.go:13 stores routing as an opaque json_util.RawMessage, so the backend never parses or reorders rules — whatever array order the frontend (or a hand-edited Advanced JSON block) produced ships unchanged to the running Xray-core process.

That matches the report: once the domain rule for Balancer1 ends up earlier in the array than the _bl_Balancer2 rule (e.g. added before the fallback was configured, or the loopback rule recreated later by an edit or by ensureMissingBalancerLoopbacks), Balancer1's fallback re-enters the routing table through the _bl_Balancer2 loopback outbound, the domain rule (no inboundTag restriction) matches again, and the connection is routed straight back into Balancer1 — an unbounded loop, consistent with the CPU spike described.

Existing validation doesn't catch this: detectBalancerCycles (balancer-loopback.ts:147-171, run from XrayPage.tsx:198 on save) only detects cycles in the balancer-to-balancer fallbackTag graph (e.g. mutual A↔B fallback). A plain, acyclic Balancer1 → Balancer2 chain — exactly the reported case — has no such cycle and passes silently.

Recommendation:
This codebase already has a precedented fix for the same class of bug: EnsureStatsRouting (internal/web/service/xray_setting.go:198-278, called from SaveXraySetting at line 29) hoists the internal api-inboundTag rule to index 0 on every save, specifically because "rules are evaluated top-to-bottom and the catch-all matches first" (its own comment cites the earlier #4113/#2818 instance of this exact problem class). The same approach — hoisting/pinning _bl_*-inboundTag loopback rules ahead of any rule targeting the parent balancer, either as a backend safety net alongside EnsureStatsRouting or by having ensureBalancerLoopback insert rather than push — would fix this at the source and cover both the structured Balancer UI and hand-edited Advanced JSON. No code is changed here; a fix would only be implemented if a maintainer requests it by mentioning @claude.

Confidence: High for the root cause and the missing safeguard, both read directly from the cited files. The exact crash mechanism inside Xray-core's dispatcher once the loop starts is outside this repo (xray-core is a separate dependency) — the CPU-spike/loop behavior itself matches the reporter's own observation.

@MHSanaei

This reply was generated automatically by an AI agent; a maintainer may follow up.

<!-- gh-comment-id:4975116425 --> @github-actions[bot] commented on GitHub (Jul 14, 2026): Investigated against the source; this is a real bug in this repo's frontend config-generation code, not Xray-core itself. **Title:** `ensureBalancerLoopback` appends the fallback loopback rule to the end of `routing.rules`, so it can land after a rule that already routes to the parent balancer **Severity:** High **Category:** Correctness **Why this matters:** `ensureBalancerLoopback` (`frontend/src/pages/xray/balancers/balancer-loopback.ts:55-87`) builds a balancer's fallback loopback rule and, when no rule for that `inboundTag` exists yet, appends it with `Array.prototype.push` (lines 81-85) unconditionally at the end of `settings.routing.rules`. It's invoked from `BalancersTab.tsx:215,238` whenever a balancer's `fallbackTag` is set to another balancer, and from `XrayPage.tsx:196` (`ensureMissingBalancerLoopbacks`) on every "Save All", which backfills any loopback rule that's missing. General rules added through the Routing tab are likewise appended (`RoutingTab.tsx:209`), so the order between a loopback rule and a general rule targeting the same balancer is purely a function of click/edit chronology — nothing enforces that the loopback rule comes first. Xray-core evaluates `routing.rules` top-to-bottom, first match wins; this repo's own docs say so explicitly (`docs/content/docs/en/operations/outbounds-routing.mdx:115`: "Rules are evaluated top-to-bottom — the first match wins"), which is also why the Routing tab offers manual drag-to-reorder. On the Go side, `internal/xray/config.go:13` stores `routing` as an opaque `json_util.RawMessage`, so the backend never parses or reorders rules — whatever array order the frontend (or a hand-edited Advanced JSON block) produced ships unchanged to the running Xray-core process. That matches the report: once the domain rule for Balancer1 ends up earlier in the array than the `_bl_Balancer2` rule (e.g. added before the fallback was configured, or the loopback rule recreated later by an edit or by `ensureMissingBalancerLoopbacks`), Balancer1's fallback re-enters the routing table through the `_bl_Balancer2` loopback outbound, the domain rule (no `inboundTag` restriction) matches again, and the connection is routed straight back into Balancer1 — an unbounded loop, consistent with the CPU spike described. Existing validation doesn't catch this: `detectBalancerCycles` (`balancer-loopback.ts:147-171`, run from `XrayPage.tsx:198` on save) only detects cycles in the balancer-to-balancer `fallbackTag` graph (e.g. mutual A↔B fallback). A plain, acyclic Balancer1 → Balancer2 chain — exactly the reported case — has no such cycle and passes silently. **Recommendation:** This codebase already has a precedented fix for the same class of bug: `EnsureStatsRouting` (`internal/web/service/xray_setting.go:198-278`, called from `SaveXraySetting` at line 29) hoists the internal `api`-inboundTag rule to index 0 on every save, specifically because "rules are evaluated top-to-bottom and the catch-all matches first" (its own comment cites the earlier #4113/#2818 instance of this exact problem class). The same approach — hoisting/pinning `_bl_*`-inboundTag loopback rules ahead of any rule targeting the parent balancer, either as a backend safety net alongside `EnsureStatsRouting` or by having `ensureBalancerLoopback` insert rather than `push` — would fix this at the source and cover both the structured Balancer UI and hand-edited Advanced JSON. No code is changed here; a fix would only be implemented if a maintainer requests it by mentioning @claude. **Confidence:** High for the root cause and the missing safeguard, both read directly from the cited files. The exact crash mechanism inside Xray-core's dispatcher once the loop starts is outside this repo (xray-core is a separate dependency) — the CPU-spike/loop behavior itself matches the reporter's own observation. @MHSanaei *This reply was generated automatically by an AI agent; a maintainer may follow up.*
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4133
No description provided.