mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #5960] [Bug]: Balancer loopback routing rules placed incorrectly in routing table causes infinite loop #4133
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4133
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
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
@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:
ensureBalancerLoopbackappends the fallback loopback rule to the end ofrouting.rules, so it can land after a rule that already routes to the parent balancerSeverity: 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 thatinboundTagexists yet, appends it withArray.prototype.push(lines 81-85) unconditionally at the end ofsettings.routing.rules. It's invoked fromBalancersTab.tsx:215,238whenever a balancer'sfallbackTagis set to another balancer, and fromXrayPage.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.rulestop-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:13storesroutingas an opaquejson_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_Balancer2rule (e.g. added before the fallback was configured, or the loopback rule recreated later by an edit or byensureMissingBalancerLoopbacks), Balancer1's fallback re-enters the routing table through the_bl_Balancer2loopback outbound, the domain rule (noinboundTagrestriction) 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 fromXrayPage.tsx:198on save) only detects cycles in the balancer-to-balancerfallbackTaggraph (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 fromSaveXraySettingat line 29) hoists the internalapi-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 alongsideEnsureStatsRoutingor by havingensureBalancerLoopbackinsert rather thanpush— 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.