[PR #6054] [MERGED] fix(balancer): pin loopback routing rules ahead of general rules #6066

Closed
opened 2026-11-02 04:06:08 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6054
Author: @H-TTTTT
Created: 7/20/2026
Status: ✅ Merged
Merged: 7/21/2026
Merged by: @MHSanaei

Base: main ← Head: fix/balancer-loopback-order


📝 Commits (2)

  • bc7bd67 fix(balancer): pin loopback routing rules ahead of general rules
  • c34ddc1 ci: re-trigger checks after flaky FuzzDecodeCertPin timeout

📊 Changes

2 files changed (+122 additions, -4 deletions)

View changed files

📝 frontend/src/pages/xray/balancers/balancer-loopback.ts (+22 -4)
📝 frontend/src/test/balancer-loopback.test.ts (+100 -0)

📄 Description

Summary

`ensureBalancerLoopback` appended a balancer's fallback loopback rule (`{ inboundTag: ['bl'], balancerTag: }`) to the end of `routing.rules` via `Array.push`, and only updated `balancerTag` in place when the rule already existed.

Xray evaluates routing rules top-to-bottom, first match wins, and a general domain rule (no `inboundTag` restriction) matches every inbound — including the loopback one. So once such a general rule targeting the parent balancer ended up earlier in the array (added before the fallback was configured, or recreated later by an edit or by `ensureMissingBalancerLoopbacks`), the fallback re-entered routing through the `bl` loopback outbound, the general rule matched again, and traffic routed straight back into the parent balancer: an unbounded loop and the CPU spike reported. `detectBalancerCycles` only catches mutual fallback cycles, so a plain acyclic `Balancer1 -> Balancer2` chain passed silently.

Changes

  • Insert the loopback rule before the first general (no-`inboundTag`) rule instead of pushing to the end.
  • Reposition any existing loopback rule that already landed after a general rule (preserving its other fields, mirroring the `EnsureStatsRouting` hoist used for the same class of bug — "rules are evaluated top-to-bottom and the catch-all matches first").
  • Rules with an `inboundTag` restriction (`api`, real inbounds) are left in place — they can never match loopback traffic, so only the unrestricted rules are dangerous.
  • `ensureMissingBalancerLoopbacks` runs on every Save All and calls `ensureBalancerLoopback` per fallback, so this also repairs configs loaded from the DB that already have the wrong order.

Verification

  • `npx vitest run src/test/balancer` — 72 passing across 4 files. New `ensureBalancerLoopback rule ordering` block covers: new rule inserted ahead of a general rule; existing mis-ordered rule repositioned; `inboundTag`-restricted rules left in place; `ensureMissingBalancerLoopbacks` repositions every mis-ordered rule; idempotent across repeated calls.
  • `npx tsc --noEmit` + `npx eslint` on the touched files — clean.

Fixes #5960


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6054 **Author:** [@H-TTTTT](https://github.com/H-TTTTT) **Created:** 7/20/2026 **Status:** ✅ Merged **Merged:** 7/21/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/balancer-loopback-order` --- ### 📝 Commits (2) - [`bc7bd67`](https://github.com/MHSanaei/3x-ui/commit/bc7bd67fa3144b392f0f6aba421b606405c457b3) fix(balancer): pin loopback routing rules ahead of general rules - [`c34ddc1`](https://github.com/MHSanaei/3x-ui/commit/c34ddc1e2f94ed94efffd7febb9d7756580c6318) ci: re-trigger checks after flaky FuzzDecodeCertPin timeout ### 📊 Changes **2 files changed** (+122 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/pages/xray/balancers/balancer-loopback.ts` (+22 -4) 📝 `frontend/src/test/balancer-loopback.test.ts` (+100 -0) </details> ### 📄 Description ## Summary \`ensureBalancerLoopback\` appended a balancer's fallback loopback rule (\`{ inboundTag: ['_bl_<target>'], balancerTag: <target> }\`) to the **end** of \`routing.rules\` via \`Array.push\`, and only updated \`balancerTag\` in place when the rule already existed. Xray evaluates routing rules **top-to-bottom, first match wins**, and a general domain rule (no \`inboundTag\` restriction) matches **every** inbound — including the loopback one. So once such a general rule targeting the parent balancer ended up earlier in the array (added before the fallback was configured, or recreated later by an edit or by \`ensureMissingBalancerLoopbacks\`), the fallback re-entered routing through the \`_bl_<target>\` loopback outbound, the general rule matched again, and traffic routed straight back into the parent balancer: an **unbounded loop** and the CPU spike reported. \`detectBalancerCycles\` only catches mutual fallback cycles, so a plain acyclic \`Balancer1 -> Balancer2\` chain passed silently. ## Changes - Insert the loopback rule **before the first general (no-\`inboundTag\`) rule** instead of pushing to the end. - **Reposition** any existing loopback rule that already landed after a general rule (preserving its other fields, mirroring the \`EnsureStatsRouting\` hoist used for the same class of bug — \"rules are evaluated top-to-bottom and the catch-all matches first\"). - Rules **with** an \`inboundTag\` restriction (\`api\`, real inbounds) are left in place — they can never match loopback traffic, so only the unrestricted rules are dangerous. - \`ensureMissingBalancerLoopbacks\` runs on every **Save All** and calls \`ensureBalancerLoopback\` per fallback, so this also **repairs configs loaded from the DB** that already have the wrong order. ## Verification - \`npx vitest run src/test/balancer\` — 72 passing across 4 files. New \`ensureBalancerLoopback rule ordering\` block covers: new rule inserted ahead of a general rule; existing mis-ordered rule repositioned; \`inboundTag\`-restricted rules left in place; \`ensureMissingBalancerLoopbacks\` repositions every mis-ordered rule; idempotent across repeated calls. - \`npx tsc --noEmit\` + \`npx eslint\` on the touched files — clean. Fixes #5960 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:06:08 +07:00
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#6066
No description provided.