mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6556] [Bug]: Subscription outbound links become incorrectly mapped after deleting and re-adding an inbound #4395
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#4395
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 @dicreat on GitHub (Sep 15, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6556
Describe the bug
Summary
After deleting and re-adding an inbound that was previously included in an outbound subscription, the subscription links become incorrectly mapped to the corresponding server locations. The Balancer Settings also retain the original link names, but the active route points to a newly created link that is not selected in the balancer configuration.
Environment
subInitial configuration
I had three outbound connections to servers in different locations:
sub-francesub-germanysub-swedenAll three outbounds were added through the Subscription functionality in the Outbounds section and worked correctly in the Balancer.
The Balancer initially contained the following three links:
sub-francesub-germanysub-swedenHow to reproduce the problem
Create three outbound connections using the Subscription functionality:
Use
subas the subscription prefix.Add all three outbounds to a Balancer.
Verify that all three links work correctly and correspond to the expected server locations.
Delete the France inbound/server.
The France entry is automatically removed from the subscription of the inbound node.
Create a new France inbound/server.
Add the new France inbound to the same subscription.
Check the resulting subscription links.
Open Balancer Settings and inspect the configured routes and active route.
Expected behavior
After deleting the France inbound and adding a new France inbound to the subscription:
The new France outbound should be correctly associated with
sub-france.The existing links should retain their original server/location mapping:
sub-france→ Francesub-germany→ Germanysub-sweden→ SwedenNo unexpected link such as
sub-sweden-1should be created unless explicitly required.The Balancer should continue to use exactly the links selected in its configuration.
The active route should always be one of the currently configured/selected balancer targets.
Actual behavior
After adding the new France inbound to the subscription, the subscription entries become incorrectly associated with the server IP addresses:
sub-germanysub-swedensub-sweden-1The expected
sub-francelink is no longer present in the subscription in the expected form.At the same time, Balancer Settings still shows the original link names:
sub-francesub-germanysub-swedenHowever, the active route is shown as
sub-sweden-1.When I edit the Balancer,
sub-sweden-1is not selected as one of the balancer targets and should not participate in load balancing.This means that the Balancer appears to be using a link that is not actually selected in its configuration.
Impact
This can lead to incorrect traffic routing because subscription link names no longer correspond to their actual server locations. More importantly, the Balancer may select and use a dynamically created link (
sub-sweden-1) that is not selected in the Balancer configuration.This makes the relationship between Subscriptions, Outbounds, and Balancer Settings inconsistent and can result in traffic being routed through an unexpected server.
Additional observation
The issue appears to be related to how subscription entries are identified and/or updated after an inbound is deleted and subsequently re-added. It looks as if the existing subscription entries may be matched by their position/index rather than by a stable identifier, causing the server addresses to shift between subscription names.
The fact that
sub-sweden-1becomes the active route while it is not selected in the Balancer Settings may indicate that the Balancer retains a stale reference to a previous outbound/subscription entry.Relevant logs
Screenshots
No response
3x-ui version
3.8.0
Xray-core version
No response
How did you install 3x-ui?
install.sh script
Operating system
Ubuntu 24
Which parts of the panel are affected?
Subscription (share links / Clash / JSON)
Browser (only if it is a UI bug)
No response
Is the panel behind a reverse proxy or CDN?
None
Before submitting
@github-actions[bot] commented on GitHub (Sep 15, 2026):
Thanks for the very precise write-up — your "Additional observation" is correct, and it is actually two separate things:
sub-sweden-1is not a 3x-ui defect. A balancerselectorentry is a tag prefix in xray-core, not an exact tag.sub-swedentherefore also selectssub-sweden-1. This is already documented and was answered before in #5560.1. Why the balancer uses a link you did not select
internal/web/service/outbound_subscription.go:642:and
docs/content/docs/en/operations/outbounds-routing.mdx:156:The panel relies on this itself —
internal/sub/json_service.go:537builds a whole balancer from a single prefix entry:So with
sub-swedenin the selector,sub-sweden-1is a legitimate member as far as xray-core is concerned, which is why it shows up as the active route while the edit dialog does not show it ticked. The Selector field in the balancer dialog (frontend/src/pages/xray/balancers/BalancerFormModal.tsx:250-255) is an AntDmode="tags"select whose options are existing tags, which makes it look like exact selection — it is not. There is no exact-match mode in the xray-core balancer API.2. Why the names stopped matching the locations
Tag assignment lives in
assignStableTags,internal/web/service/outbound_subscription.go:472. Three steps, in order:Step 2 is the one that bites you.
prevTagByIndexis built purely from the array position of the previous fetch (lines 408-420), so when France disappears from the upstream subscription and something else lands on its index, that entry inherits France's tag. The-1suffix you see comes from the collision loop at lines 513-515, sosub-sweden-1is a tag that was pushed aside, not a deliberately created server.This is intentional as a best-effort heuristic —
TestAssignStableTags/falls_back_to_the_previous_tag_at_the_same_position(internal/web/service/outbound_subscription_test.go:194) pins exactly that behaviour — but it is also the mechanism by which a tag ends up pointing at a different country.What v3.8.0 already fixed: commit
8abe87b6("fix(outbounds): preserve stable subscription tags", PR #6345, shipped in v3.8.0) addedreservedStableTagsso an inserted link can no longer steal the tag of a server that is still in the list. That is the same family as #6341 (Netherlands/Netherlands-1).What it did not fix — and is why your panel does not self-heal: the identity→tag map is persisted at lines 429-433/445 into the
link_identitiescolumn, and step 1 (line 489) reuses it unconditionally on every refresh. Once a wrong pairing is in there, it is a fixed point: every subsequent refresh reproduces it. The column isjson:"-"(internal/database/model/model.go:1260),Updatenever touches it (lines 211-243), and nothing in the UI or API can reset it. v3.8.0 was released one day before you filed this, so the swap you are looking at was almost certainly produced by the pre-v3.8.0 algorithm and then frozen in the database.A second, smaller inconsistency at the same spot:
link_identitiesis written from the unfiltered parse (line 430 iteratesidentities) whilelast_fetched_outbounds— the source ofprevTagByIndex— is written from the filteredkeptslice (lines 439-444). If any outbound is dropped byfilterOutboundsRejectedByCore, the positional map is off by one from then on.How to get your panel back to a correct state
Reset the subscription's stored mapping. Easiest: delete the outbound subscription in Xray → Outbounds → Subscriptions and add it again with the same URL and prefix
sub.Delete(line 246) hard-deletes the row, so the next refresh allocates every tag fresh from the remarks (sub-france,sub-germany,sub-sweden).If you prefer to keep the subscription row and its settings, clear just the two columns:
Then hit Refresh on the subscription.
Re-select the balancer members afterwards, since the tags were re-allocated — and make sure no remaining tag is a prefix of another (
sub-swedenvssub-sweden-1), or the balancer will keep pulling in the longer one.One thing I could not determine
Your exact table (
sub-germany→ France,sub-sweden→ Germany,sub-sweden-1→ Sweden) is a clean one-position shift, which the algorithm only produces whenprevis empty at that refresh whileprevTagByIndexstill holds the older, shorter list. I could not reproduce that from the v3.8.0 code path with an intact identity map, so I cannot tell whether your panel hit a still-open hole or is showing pre-v3.8.0 damage. The single thing that settles it is the current content of that row — please paste the output of (redact hosts/UUIDs, they appear in the identity strings):If
link_identitiesis empty/{}whilelast_fetched_outboundshas entries, that is a live bug and pins it exactly. I have labelled this "clarification needed" for that reason; everything above stands either way.Verdict: Confirmed bug
Severity: Medium
Confidence: Medium
Root cause:
assignStableTagspositional fallback,internal/web/service/outbound_subscription.go:495— a link whose identity is unknown inherits the tag that sat at its index in the previous fetch, so removing a server and adding another re-points an existing tag at a different physical server. Compounded by the persisted identity→tag map (link_identities, written at lines 429-433/445, reused unconditionally at line 489) having no reset path:json:"-"atinternal/database/model/model.go:1260, untouched byUpdateat lines 211-243. Introduced with the feature in0daedd3d(feat: add support for subscription-based outbounds with auto-update, #5037).Already fixed: Partially —
8abe87b6("fix(outbounds): preserve stable subscription tags", #6345) is in v3.8.0 and stops an inserted link from stealing the tag of a server still present in the batch. The positional fallback itself, the unfiltered/filtered index mismatch (lines 430 vs 439-444) and the missing repair path for an already-swappedlink_identitiesare all still present on the default branch.Duplicate of: None. Related: #5560 (balancer selector is a prefix match — same explanation for the
sub-sweden-1active route), #6341 (theNetherlands-1duplicate, fixed by the same commit).Evidence:
internal/web/service/outbound_subscription.go:495if old, ok := prevTagByIndex[i]; ok && old != "" && !reservedStableTags[old] {; lines 513-515for k := 1; used[final] || (reservedStableTags[final] && final != identityTag); k++ { final = fmt.Sprintf("%s-%d", candidate, k) }(source of the-1suffix); lines 408-420 buildprevTagByIndexfrom array position only; lines 439-444 persist the filteredkeptslice while line 430 persists identities for the unfiltered parse;internal/database/model/model.go:1260LinkIdentities string \json:"-"`; testTestAssignStableTags/falls_back_to_the_previous_tag_at_the_same_positionatinternal/web/service/outbound_subscription_test.go:194pins the positional fallback as intended;internal/sub/json_service.go:537anddocs/content/docs/en/operations/outbounds-routing.mdx:156establish that balancer selectors are prefixes;git tag --contains8abe87b6→v3.8.0,dev-latest. Not determined: Whether the reporter's exact one-position shift is reachable on v3.8.0 or is pre-v3.8.0 state frozen in the database. Settled by thelink_identities/last_fetched_outbounds` values of that row (query above).Suggested fix: Reserve the previous tag set, not just the tags of identities still in the batch, so a brand-new link never inherits a removed link's tag via
prevTagByIndex— or drop the positional fallback for links whose identity was never seen. BuildingprevTagByIndexfrom the same unfiltered list thatlink_identitiesis built from would also remove the off-by-one after a core-rejected outbound. Separately, a way to reset a subscription's stored identity map without deleting the subscription would let users out of an already-swapped state.This reply was generated automatically; a maintainer may follow up.
@dicreat commented on GitHub (Sep 15, 2026):
sqlite3 /etc/x-ui/x-ui.db
"SELECT id, tag_prefix, link_identities, last_fetched_outbounds FROM outbound_subscriptions;"
3|sub|
{
"vless:vless:// ... &type=tcp": "sub-sweden-1",
"vless:vless:// .... &type=tcp": "sub-germany",
"vless:vless:// ... &type=tcp": "sub-sweden"
}
@github-actions[bot] commented on GitHub (Sep 15, 2026):
Thanks — that row settles it, and it upgrades the finding.
Two things are now confirmed: (1) the wrong mapping is frozen in your database, which is why upgrading did not repair it, and (2) I can now name the mechanism that let it happen on v3.8.0 — for Reality inbounds a 3x-ui subscription serves a different link on every request, so the consuming panel never recognises a server it already knows and falls back to matching by list position.
1. What your row shows
The JSON you pasted is the third column,
link_identities— the identity → tag map written atinternal/web/service/outbound_subscription.go:445(sub.LinkIdentities = string(identJSON)). It holds three entries and nosub-france:sub-sweden-1,sub-germany,sub-sweden.That map is reused unconditionally on every refresh,
outbound_subscription.go:489:So the mapping you are seeing is a fixed point: each refresh reproduces it exactly. Nothing in the UI or API clears that column (
LinkIdentities string \json:"-"`—internal/database/model/model.go:1260`), which is why the panel cannot self-heal. That part of my earlier comment stands.2. Why identity matching failed in the first place
A vless link's identity is the whole canonical link minus the remark —
internal/util/link/outbound.go:282:and
params := u.Query()(line 268) is the full query string —sidandsniincluded.But 3x-ui's own subscription server picks both of those at random on every single request for a reality inbound,
internal/sub/service.go:1923and:1931:(the JSON subscription does the same at
internal/sub/json_service.go:795and:801). And a freshly created reality inbound gets eight shortIds by default —frontend/src/utils/index.ts:230-232:written into
shortIdswhenever the security dropdown is switched to reality (frontend/src/pages/inbounds/form/useSecurityActions.ts:337).The consequence chain, all on current
main:prevnever matches.reservedStableTagsis built only from identities found inprev(outbound_subscription.go:476), so it stays empty — the v3.8.0 guard from8abe87b6never engages.outbound_subscription.go:495:-1(outbound_subscription.go:512-515).Step 4 with the new France landing at index 0 produces your table exactly: index 0 inherits
sub-germany, index 1 inheritssub-sweden, index 2 has no previous tag at that index, solink.SuggestTag("sub", "Sweden", 2)→sub-sweden, which is already used →sub-sweden-1.What I still cannot confirm: that your upstream is reality-with-multiple-
sid/sni. Your identity strings are redacted exactly wheresid=would be. One check — run this twice and compare:Different values between the two runs means this is your case.
3. Getting your panel back — and keeping it there
Clear the stored state. As before, and it must be both columns, since
last_fetched_outboundsis what drives the positional fallback:Then press Refresh on the subscription. Deleting and re-adding the subscription has the same effect.
Stop the churn at the source, if the upstream panels are yours. On each upstream reality inbound, keep a single shortId and a single SNI.
random.Num(1)always returns0, so the served link becomes byte-stable, identity reuse starts working, and tags then survive an add/remove. Without this, the same drift returns the next time a server is added or removed upstream.Re-select the balancer members afterwards, and avoid leaving
sub-swedenandsub-sweden-1side by side — a balancer selector is a tag prefix, which is the separate reasonsub-sweden-1showed up as the active route (my earlier comment, and #5560).@MHSanaei — flagging this one: the positional fallback is not a rare last resort, it is the only path taken whenever the upstream is a default 3x-ui reality inbound.
Verdict: Confirmed bug
Severity: High
Confidence: High
Root cause: Subscription tag stability depends on the link text being identical between fetches —
internal/util/link/outbound.go:282builds the identity fromcanonicalQuery(params), i.e. the full query string — butinternal/sub/service.go:1923and:1931re-randomisesniandsidon every request for a reality inbound, andfrontend/src/utils/index.ts:230-232gives a new reality inbound eight shortIds by default.prevtherefore never matches,reservedStableTags(internal/web/service/outbound_subscription.go:476) stays empty, the8abe87b6guard never fires, and every tag is assigned by list position atinternal/web/service/outbound_subscription.go:495. Any insertion or removal above the end of the list then re-points existing tags — and the balancers that select them — at physically different servers. The identity feature dates to0daedd3d(feat: subscription-based outbounds with auto-update, #5037).Already fixed: No.
8abe87b6("fix(outbounds): preserve stable subscription tags", #6345, in v3.8.0) only protects tags of identities that are still matched, so it is inert on this path. Still present on the default branch; the only commits on the file since are9f07951b(user agents).Duplicate of: None. Related: #5560 (balancer selector is a prefix match — explains the
sub-sweden-1active route), #6341 (Netherlands-1, same family, fixed by8abe87b6).Evidence:
internal/util/link/outbound.go:268params := u.Query()and:282identity := "vless:" + ... + "?" + canonicalQuery(params);internal/sub/service.go:1923params["sni"], _ = sNames[random.Num(len(sNames))].(string)and:1931params["sid"], _ = shortIds[random.Num(len(shortIds))].(string);internal/sub/json_service.go:795/:801(same for the JSON sub);frontend/src/utils/index.ts:230-232(eight shortIds) consumed atfrontend/src/pages/inbounds/form/useSecurityActions.ts:337;internal/web/service/outbound_subscription.go:476(reserved set built only from matched identities),:489(unconditional identity reuse),:495(positional fallback),:512-515(the-Ncollision suffix),:445(the map is persisted);internal/database/model/model.go:1260LinkIdentities string \json:"-"`— no reset path; reporter's row:link_identitiesholds three entries, none of themsub-france.deriveSpiderX(internal/sub/service.go:1963) shows the deterministic-per-client pattern already exists in this file. Not determined: Whether this reporter's upstream is specifically a reality inbound with more than one shortId/SNI — thesid=part of their identity strings is redacted. Settled by fetching the subscription URL twice and diffing thesid=/sni=` values.Suggested fix: Exclude the parameters the subscription server deliberately randomises (
sid,sni) from the identity string inParseLink, or make the subscription's choice deterministic per client the wayderiveSpiderXalready does — either one restores identity reuse and takes the positional fallback off the common path. Independently, reserving the entire previous tag set rather than only the tags of still-matched identities would stop a new link from inheriting a removed link's tag. A way to clear a subscription's stored mapping from the UI would let users out of an already-swapped state without hand-editing the database.This reply was generated automatically; a maintainer may follow up.
@dicreat commented on GitHub (Sep 15, 2026):