[GH-ISSUE #5502] [Bug]: Inbound tag changes are not synchronized with routing rules and can crash Xray #3843

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

Originally created by @atem32 on GitHub (Jun 23, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5502

Describe the bug

Inbound tags are not stable and are not synchronized with routing rules.

There are two related cases with the same root problem:

  • Existing inbounds with the old tag schema can be automatically changed to the new generated tag schema after editing and saving the inbound.
  • Custom inbound tags can be reset back to the default generated tag after editing and saving any unrelated inbound setting.

In both cases, routing rules are not updated when the inbound tag changes. As a result, the generated Xray config may contain routing rules with inboundTag values that no longer exist in the inbound list.

This can cause xray-core to consume excessive CPU and memory and eventually crash or keep restarting until routing rules are manually fixed.

Example old tag:

"tag": "inbound-21751"

Example new generated tag:

 "tag": "in-21751-udp"

Example custom tag:

"tag": "cn-home-in-443-tcp"

How to reproduce the problem

Case A: old tag schema is changed to the new generated tag schema

Start with an existing inbound that uses the old tag schema:

"tag": "inbound-21751"

Use this tag in routing rules:

"inboundTag": ["inbound-21751"]

Upgrade 3x-ui to a version where the new inbound tag schema is used.

Open the existing inbound in the panel, change any inbound setting, for example MTU, and save the inbound.

After saving, the panel changes the inbound tag to the new generated format:

"tag": "in-21751-udp"

However, routing rules still contain the old tag:

"inboundTag": ["inbound-21751"]

As a result, routing rules reference a non-existing inbound tag.

Case B: custom tag is reset back to the default generated tag

Open an inbound with the new default tag schema:

"tag": "in-443-tcp"

Go to:

Inbound -> Edit -> Advanced

Manually change the tag to a custom value:

"tag": "cn-home-in-443-tcp"

Save the inbound.

Update routing rules to use the custom tag:

"inboundTag": ["cn-home-in-443-tcp"]

At this point, the configuration works.

Then open the same inbound again, change any unrelated inbound parameter, and save the inbound.

After saving, the panel resets the custom tag back to the default generated tag:

"tag": "in-443-tcp"

However, routing rules still contain the custom tag:

"inboundTag": ["cn-home-in-443-tcp"]

As a result, routing rules again reference a non-existing inbound tag.

Expected behavior

Inbound tags should be treated as stable identifiers.

If the panel changes, migrates, regenerates, or resets an inbound tag, all routing rules referencing the previous tag should be updated automatically.

For example, if the inbound tag changes from:

"inbound-21751"

to:

"in-21751-udp"

then routing rules should also be migrated from:

"inboundTag": ["inbound-21751"]

to:

"inboundTag": ["in-21751-udp"]

If the user manually sets a custom inbound tag in Inbound -> Edit -> Advanced, the panel should preserve this custom tag and must not silently reset it to the default generated tag when unrelated inbound settings are changed.

Before applying or restarting Xray, 3x-ui should also validate that every routing.rules[].inboundTag[] value exists in the current inbound list.

If a routing rule references a non-existing inbound tag, the panel should refuse to apply the config and show an error similar to this:

Routing rule references non-existing inbound tag: inbound-21751

It would also be useful to add a configuration migration mechanism between versions, so old inbound tags and routing rules are migrated together.

Additionally, it would be useful to add optional support for systemd resource limits for x-ui.service, so a broken config cannot consume all server resources:

[Service]
MemoryAccounting=true
MemoryMax=700M
CPUAccounting=true
CPUQuota=80%
TasksMax=512
Restart=on-failure
RestartSec=5

Actual behavior

The panel can automatically change or reset inbound tags, but routing rules are not updated.

This creates an invalid or inconsistent Xray config where routing rules reference inbound tags that no longer exist.

Example invalid config after old tag migration:

{
  "inbounds": [
    {
      "listen": "",
      "port": 21751,
      "protocol": "wireguard",
      "tag": "in-21751-udp"
    }
  ],
  "routing": {
    "rules": [
      {
        "type": "field",
        "inboundTag": ["inbound-21751"],
        "outboundTag": "some-outbound"
      }
    ]
  }
}

Example invalid config after custom tag reset:

{
  "inbounds": [
    {
      "port": 443,
      "protocol": "vless",
      "tag": "in-443-tcp"
    }
  ],
  "routing": {
    "rules": [
      {
        "type": "field",
        "inboundTag": ["cn-home-in-443-tcp"],
        "outboundTag": "some-outbound"
      }
    ]
  }
}

In both examples, routing rules point to tags that do not exist anymore.

After applying this config, xray-core may start consuming excessive CPU and memory and eventually crash or keep restarting. The issue continues until routing rules are manually updated to match the current inbound tag.

Relevant logs

I do not have full logs at the moment.

The main evidence is in the generated config: `routing.rules[].inboundTag[]` references tags that do not exist in `inbounds[].tag`.

Visible behavior:

    xray-core consumes excessive CPU and memory after applying a config where routing rules reference non-existing inbound tags.
    xray-core eventually crashes or keeps restarting until routing rules are manually fixed.

Example invalid generated config:

    {
      "inbounds": [
        {
          "listen": "",
          "port": 21751,
          "protocol": "wireguard",
          "tag": "in-21751-udp"
        }
      ],
      "routing": {
        "rules": [
          {
            "type": "field",
            "inboundTag": ["inbound-21751"],
            "outboundTag": "some-outbound"
          }
        ]
      }
    }

Screenshots

No screenshots available at the moment.

The issue can be verified by comparing inbounds[].tag with routing.rules[].inboundTag[] after editing and saving an inbound.

3x-ui version

3.3.1

Xray-core version

26.6.1

How did you install 3x-ui?

install.sh script

Operating system

Debian 12

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?

None

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 @atem32 on GitHub (Jun 23, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5502 ### Describe the bug Inbound tags are not stable and are not synchronized with routing rules. There are two related cases with the same root problem: - Existing inbounds with the old tag schema can be automatically changed to the new generated tag schema after editing and saving the inbound. - Custom inbound tags can be reset back to the default generated tag after editing and saving any unrelated inbound setting. In both cases, routing rules are not updated when the inbound tag changes. As a result, the generated Xray config may contain routing rules with `inboundTag` values that no longer exist in the inbound list. This can cause `xray-core` to consume excessive CPU and memory and eventually crash or keep restarting until routing rules are manually fixed. Example old tag: "tag": "inbound-21751" Example new generated tag: "tag": "in-21751-udp" Example custom tag: "tag": "cn-home-in-443-tcp" ### How to reproduce the problem ### Case A: old tag schema is changed to the new generated tag schema Start with an existing inbound that uses the old tag schema: "tag": "inbound-21751" Use this tag in routing rules: "inboundTag": ["inbound-21751"] Upgrade `3x-ui` to a version where the new inbound tag schema is used. Open the existing inbound in the panel, change any inbound setting, for example MTU, and save the inbound. After saving, the panel changes the inbound tag to the new generated format: "tag": "in-21751-udp" However, routing rules still contain the old tag: "inboundTag": ["inbound-21751"] As a result, routing rules reference a non-existing inbound tag. ### Case B: custom tag is reset back to the default generated tag Open an inbound with the new default tag schema: "tag": "in-443-tcp" Go to: Inbound -> Edit -> Advanced Manually change the tag to a custom value: "tag": "cn-home-in-443-tcp" Save the inbound. Update routing rules to use the custom tag: "inboundTag": ["cn-home-in-443-tcp"] At this point, the configuration works. Then open the same inbound again, change any unrelated inbound parameter, and save the inbound. After saving, the panel resets the custom tag back to the default generated tag: "tag": "in-443-tcp" However, routing rules still contain the custom tag: "inboundTag": ["cn-home-in-443-tcp"] As a result, routing rules again reference a non-existing inbound tag. ### Expected behavior Inbound tags should be treated as stable identifiers. If the panel changes, migrates, regenerates, or resets an inbound tag, all routing rules referencing the previous tag should be updated automatically. For example, if the inbound tag changes from: "inbound-21751" to: "in-21751-udp" then routing rules should also be migrated from: "inboundTag": ["inbound-21751"] to: "inboundTag": ["in-21751-udp"] If the user manually sets a custom inbound tag in `Inbound -> Edit -> Advanced`, the panel should preserve this custom tag and must not silently reset it to the default generated tag when unrelated inbound settings are changed. Before applying or restarting Xray, `3x-ui` should also validate that every `routing.rules[].inboundTag[]` value exists in the current inbound list. If a routing rule references a non-existing inbound tag, the panel should refuse to apply the config and show an error similar to this: Routing rule references non-existing inbound tag: inbound-21751 It would also be useful to add a configuration migration mechanism between versions, so old inbound tags and routing rules are migrated together. Additionally, it would be useful to add optional support for systemd resource limits for `x-ui.service`, so a broken config cannot consume all server resources: [Service] MemoryAccounting=true MemoryMax=700M CPUAccounting=true CPUQuota=80% TasksMax=512 Restart=on-failure RestartSec=5 ### Actual behavior The panel can automatically change or reset inbound tags, but routing rules are not updated. This creates an invalid or inconsistent Xray config where routing rules reference inbound tags that no longer exist. Example invalid config after old tag migration: { "inbounds": [ { "listen": "", "port": 21751, "protocol": "wireguard", "tag": "in-21751-udp" } ], "routing": { "rules": [ { "type": "field", "inboundTag": ["inbound-21751"], "outboundTag": "some-outbound" } ] } } Example invalid config after custom tag reset: { "inbounds": [ { "port": 443, "protocol": "vless", "tag": "in-443-tcp" } ], "routing": { "rules": [ { "type": "field", "inboundTag": ["cn-home-in-443-tcp"], "outboundTag": "some-outbound" } ] } } In both examples, routing rules point to tags that do not exist anymore. After applying this config, `xray-core` may start consuming excessive CPU and memory and eventually crash or keep restarting. The issue continues until routing rules are manually updated to match the current inbound tag. ### Relevant logs ```shell I do not have full logs at the moment. The main evidence is in the generated config: `routing.rules[].inboundTag[]` references tags that do not exist in `inbounds[].tag`. Visible behavior: xray-core consumes excessive CPU and memory after applying a config where routing rules reference non-existing inbound tags. xray-core eventually crashes or keeps restarting until routing rules are manually fixed. Example invalid generated config: { "inbounds": [ { "listen": "", "port": 21751, "protocol": "wireguard", "tag": "in-21751-udp" } ], "routing": { "rules": [ { "type": "field", "inboundTag": ["inbound-21751"], "outboundTag": "some-outbound" } ] } } ``` ### Screenshots No screenshots available at the moment. The issue can be verified by comparing `inbounds[].tag` with `routing.rules[].inboundTag[]` after editing and saving an inbound. ### 3x-ui version 3.3.1 ### Xray-core version 26.6.1 ### How did you install 3x-ui? install.sh script ### Operating system Debian 12 ### 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? None ### 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:35:20 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 23, 2026):

Both cases are confirmed against the source. Here is what the investigation found and what the current development branch has already addressed.

Root cause in v3.3.1

In v3.3.1, UpdateInbound in internal/web/service/inbound.go had no mechanism to distinguish an auto-generated tag from a custom or legacy-schema tag. When the form was submitted:

  • The backend would re-derive the tag from (port, protocol, transport) using the current schema (e.g. in-21751-udp), overwriting whatever the stored tag was — including old-schema tags such as inbound-21751 and user-set tags such as cn-home-in-443-tcp.
  • After the tag changed, nothing in the codebase updated the routing.rules[].inboundTag values stored in xrayTemplateConfig. The routing rules were left pointing at the now-deleted tag, producing the invalid config you described.

What the development branch already fixes

The commit at HEAD (beyond v3.3.1) introduces three coordinated changes, none of which are in the released v3.3.1 binary:

  1. internal/web/service/port_conflict.go — isAutoGeneratedTag() classifies a stored tag as auto-generated only when it exactly matches the canonical in-<port>-<transport> pattern (or n<N>-in-... for node-scoped inbounds). Old-schema tags (inbound-NNNNN) and any tag that does not match that shape are treated as custom and are preserved verbatim across saves.

  2. internal/web/service/inbound.go — UpdateInbound checks isAutoGeneratedTag on the stored tag before deciding whether to regenerate. If the stored tag was auto-generated and the port/protocol/transport did not change, it regenerates; otherwise the stored tag is preserved. Unit test TestUpdateInbound_KeepsCustomTagOnPortChange in internal/web/service/inbound_update_tag_test.go covers the custom-tag case explicitly.

  3. internal/web/service/xray_setting_routing_sync.go — new file with two functions:

    • PropagateInboundTagRename(oldTag, newTag): called from UpdateInbound immediately after a tag does change. It rewrites every inboundTag reference in xrayTemplateConfig routing rules and loopback outbounds from oldTag to newTag, then persists the updated template. If routing changed, needRestart is set so Xray reloads the corrected config.
    • RemoveInboundTagReferences(deletedTag): called from DelInbound. Rules whose only matcher was the deleted inbound tag are removed; rules with additional matchers (IP, domain, etc.) keep the rule but lose the stale tag.

Both functions are exercised by integration tests in internal/web/service/xray_setting_routing_sync_test.go, including TestUpdateInbound_PropagatesRoutingRuleOnPortChange, which confirms that needRestart is returned correctly when routing rules are updated.

What is not yet addressed

The following requests from the issue are not implemented in the current source and remain open:

  • Pre-apply validation: there is no check before restarting Xray that verifies every routing.rules[].inboundTag[] value exists in the current inbound list. Xray-core silently ignores unknown tags in some versions but the specific CPU/memory spike you observed suggests it does not in yours.
  • Systemd resource limits: the x-ui.service unit files (x-ui.service.x86_64 etc.) do not include MemoryMax, CPUQuota, or TasksMax directives.

Workaround for v3.3.1

Until a release containing the above fix is available, the safest path is:

  1. After editing and saving an inbound, open Xray Settings in the panel and manually verify that every inboundTag in the routing rules matches an existing inbound tag.
  2. If you have already accumulated broken rules, correct them in the Xray Settings JSON editor before restarting Xray.
  3. To find the current inbound tags: x-ui → the management menu lists them, or query the API at /panel/api/inbounds/list.

If Xray is already stuck in a CPU/memory loop, stop it first (systemctl stop x-ui), correct the xrayTemplateConfig JSON via the panel's Xray Settings page or by editing the DB directly, then restart.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:4779038971 --> @github-actions[bot] commented on GitHub (Jun 23, 2026): Both cases are confirmed against the source. Here is what the investigation found and what the current development branch has already addressed. **Root cause in v3.3.1** In v3.3.1, `UpdateInbound` in `internal/web/service/inbound.go` had no mechanism to distinguish an auto-generated tag from a custom or legacy-schema tag. When the form was submitted: - The backend would re-derive the tag from `(port, protocol, transport)` using the current schema (e.g. `in-21751-udp`), overwriting whatever the stored tag was — including old-schema tags such as `inbound-21751` and user-set tags such as `cn-home-in-443-tcp`. - After the tag changed, nothing in the codebase updated the `routing.rules[].inboundTag` values stored in `xrayTemplateConfig`. The routing rules were left pointing at the now-deleted tag, producing the invalid config you described. **What the development branch already fixes** The commit at HEAD (beyond v3.3.1) introduces three coordinated changes, none of which are in the released v3.3.1 binary: 1. `internal/web/service/port_conflict.go` — `isAutoGeneratedTag()` classifies a stored tag as auto-generated only when it exactly matches the canonical `in-<port>-<transport>` pattern (or `n<N>-in-...` for node-scoped inbounds). Old-schema tags (`inbound-NNNNN`) and any tag that does not match that shape are treated as custom and are preserved verbatim across saves. 2. `internal/web/service/inbound.go` — `UpdateInbound` checks `isAutoGeneratedTag` on the stored tag before deciding whether to regenerate. If the stored tag was auto-generated **and** the port/protocol/transport did not change, it regenerates; otherwise the stored tag is preserved. Unit test `TestUpdateInbound_KeepsCustomTagOnPortChange` in `internal/web/service/inbound_update_tag_test.go` covers the custom-tag case explicitly. 3. `internal/web/service/xray_setting_routing_sync.go` — new file with two functions: - `PropagateInboundTagRename(oldTag, newTag)`: called from `UpdateInbound` immediately after a tag does change. It rewrites every `inboundTag` reference in `xrayTemplateConfig` routing rules and loopback outbounds from `oldTag` to `newTag`, then persists the updated template. If routing changed, `needRestart` is set so Xray reloads the corrected config. - `RemoveInboundTagReferences(deletedTag)`: called from `DelInbound`. Rules whose only matcher was the deleted inbound tag are removed; rules with additional matchers (IP, domain, etc.) keep the rule but lose the stale tag. Both functions are exercised by integration tests in `internal/web/service/xray_setting_routing_sync_test.go`, including `TestUpdateInbound_PropagatesRoutingRuleOnPortChange`, which confirms that `needRestart` is returned correctly when routing rules are updated. **What is not yet addressed** The following requests from the issue are not implemented in the current source and remain open: - **Pre-apply validation**: there is no check before restarting Xray that verifies every `routing.rules[].inboundTag[]` value exists in the current inbound list. Xray-core silently ignores unknown tags in some versions but the specific CPU/memory spike you observed suggests it does not in yours. - **Systemd resource limits**: the `x-ui.service` unit files (`x-ui.service.x86_64` etc.) do not include `MemoryMax`, `CPUQuota`, or `TasksMax` directives. **Workaround for v3.3.1** Until a release containing the above fix is available, the safest path is: 1. After editing and saving an inbound, open **Xray Settings** in the panel and manually verify that every `inboundTag` in the routing rules matches an existing inbound tag. 2. If you have already accumulated broken rules, correct them in the Xray Settings JSON editor before restarting Xray. 3. To find the current inbound tags: `x-ui` → the management menu lists them, or query the API at `/panel/api/inbounds/list`. If Xray is already stuck in a CPU/memory loop, stop it first (`systemctl stop x-ui`), correct the `xrayTemplateConfig` JSON via the panel's Xray Settings page or by editing the DB directly, then restart. *This reply was generated automatically; 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#3843
No description provided.