[GH-ISSUE #5870] [Bug]: Client update API creates duplicate client and then returns Duplicate subId error in v3.4.2 #4076

Closed
opened 2026-10-31 20:36:58 +07:00 by bsakaguchi · 2 comments
Owner

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

Describe the bug

Describe the bug

In 3x-ui v3.4.2, when updating an existing client through the new Clients API endpoint:

POST /panel/api/clients/update/{email}

the API updates the original client, but it also creates a new duplicate client with the old/original client data.

For example, when I update only the client's email or comment, the expected behavior is to update the same existing client. However, after the API request, I can see multiple clients with the same original email/subId-related data in the panel.

This issue only happens when using the API. If I edit the same client from the web UI, the problem does not happen.

The API documentation says this endpoint should update an existing client by email and that the request body is the full JSON client payload, not a patch request. So I send the full client payload, but the API still creates an extra client instead of only replacing/updating the existing one.

After the first update creates a duplicate client, trying to update the same client again returns a Duplicate subId error. However, even when this error is returned, another duplicate client may still be created.

This can cause duplicate clients, duplicate subscription IDs, incorrect client list data, and unexpected behavior in production panels.

How to reproduce the problem

How to reproduce the problem

  1. Install or upgrade to 3x-ui v3.4.2.

  2. Create a normal client in the panel.

  3. Confirm the client exists only once in the client list.

  4. Use the new Clients API update endpoint:

    POST /panel/api/clients/update/{email}

  5. Send a full JSON client payload and change only simple fields such as email or comment.

Example request:

POST <panel-base-url>/panel/api/clients/update/Ifif-7e5djjjhfff
Content-Type: application/json


{
  "id": "d669fc1d-e280-41c8-ada8-ba2984b73fb0",
  "email": "Ifif-7e5djjjhfffsss",
  "totalGB": 1073741824,
  "expiryTime": 1786203000000,
  "tgId": 0,
  "subid": "7e5djjjh",
  "comment": "sar88 - sss",
  "limitIp": 0,
  "tgid": 759865,
  "enable": true
}

### Expected behavior

{
  "id": "d669fc1d-e280-41c8-ada8-ba2984b73fb0",
  "email": "Ifif-7e5djjjhfffsss",
  "totalGB": 1073741824,
  "expiryTime": 1786203000000,
  "tgId": 0,
  "subid": "7e5djjjh",
  "comment": "sar88 - sss",
  "limitIp": 0,
  "tgid": 759865,
  "enable": true
}




---

## Expected behavior

```markdown
### Expected behavior

The API endpoint:

`POST /panel/api/clients/update/{email}`

should only update the existing client identified by `{email}`.

Expected behavior:

- No new client should be created.
- The existing client should keep its identity and attachments.
- Updating fields such as `email`, `comment`, `totalGB`, `expiryTime`, `enable`, etc. should only modify the target client.
- The `subid` should not be treated as a duplicate when it belongs to the same client being updated.
- The behavior should be consistent with the web UI client edit action.

### Actual behavior

### Actual behavior

When updating a client through the API:

- The original client is edited.
- A new duplicate client is also created.
- The new duplicate client contains the previous/original client information.
- On the next update attempt, the API returns a `Duplicate subId` error.
- Even when the API returns the `Duplicate subId` error, another duplicate client can still be created.
- This problem does not happen when editing the client from the web UI.

Example error response:

```json
{
  "success": false,
  "msg": "Something went wrong (Duplicate subId: 7e5djjjh\n)",
  "obj": null
}

### Relevant logs

```shell

Screenshots

Image

3x-ui version

3.4.2

Xray-core version

26.6.27

How did you install 3x-ui?

install.sh script

Operating system

ubuntu 26

Which parts of the panel are affected?

Backend (API endpoints, login, settings)

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 @HamedSanaei on GitHub (Jul 9, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/5870 ### Describe the bug ### Describe the bug In 3x-ui `v3.4.2`, when updating an existing client through the new Clients API endpoint: `POST /panel/api/clients/update/{email}` the API updates the original client, but it also creates a new duplicate client with the old/original client data. For example, when I update only the client's `email` or `comment`, the expected behavior is to update the same existing client. However, after the API request, I can see multiple clients with the same original email/subId-related data in the panel. This issue only happens when using the API. If I edit the same client from the web UI, the problem does not happen. The API documentation says this endpoint should update an existing client by email and that the request body is the full JSON client payload, not a patch request. So I send the full client payload, but the API still creates an extra client instead of only replacing/updating the existing one. After the first update creates a duplicate client, trying to update the same client again returns a `Duplicate subId` error. However, even when this error is returned, another duplicate client may still be created. This can cause duplicate clients, duplicate subscription IDs, incorrect client list data, and unexpected behavior in production panels. ### How to reproduce the problem ### How to reproduce the problem 1. Install or upgrade to 3x-ui `v3.4.2`. 2. Create a normal client in the panel. 3. Confirm the client exists only once in the client list. 4. Use the new Clients API update endpoint: `POST /panel/api/clients/update/{email}` 5. Send a full JSON client payload and change only simple fields such as `email` or `comment`. Example request: ```http POST <panel-base-url>/panel/api/clients/update/Ifif-7e5djjjhfff Content-Type: application/json { "id": "d669fc1d-e280-41c8-ada8-ba2984b73fb0", "email": "Ifif-7e5djjjhfffsss", "totalGB": 1073741824, "expiryTime": 1786203000000, "tgId": 0, "subid": "7e5djjjh", "comment": "sar88 - sss", "limitIp": 0, "tgid": 759865, "enable": true } ### Expected behavior { "id": "d669fc1d-e280-41c8-ada8-ba2984b73fb0", "email": "Ifif-7e5djjjhfffsss", "totalGB": 1073741824, "expiryTime": 1786203000000, "tgId": 0, "subid": "7e5djjjh", "comment": "sar88 - sss", "limitIp": 0, "tgid": 759865, "enable": true } --- ## Expected behavior ```markdown ### Expected behavior The API endpoint: `POST /panel/api/clients/update/{email}` should only update the existing client identified by `{email}`. Expected behavior: - No new client should be created. - The existing client should keep its identity and attachments. - Updating fields such as `email`, `comment`, `totalGB`, `expiryTime`, `enable`, etc. should only modify the target client. - The `subid` should not be treated as a duplicate when it belongs to the same client being updated. - The behavior should be consistent with the web UI client edit action. ### Actual behavior ### Actual behavior When updating a client through the API: - The original client is edited. - A new duplicate client is also created. - The new duplicate client contains the previous/original client information. - On the next update attempt, the API returns a `Duplicate subId` error. - Even when the API returns the `Duplicate subId` error, another duplicate client can still be created. - This problem does not happen when editing the client from the web UI. Example error response: ```json { "success": false, "msg": "Something went wrong (Duplicate subId: 7e5djjjh\n)", "obj": null } ### Relevant logs ```shell ``` ### Screenshots <img width="1094" height="309" alt="Image" src="https://github.com/user-attachments/assets/ab0118db-558a-4b09-9401-be3447fc2452" /> ### 3x-ui version 3.4.2 ### Xray-core version 26.6.27 ### How did you install 3x-ui? install.sh script ### Operating system ubuntu 26 ### Which parts of the panel are affected? Backend (API endpoints, login, settings) ### 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:36:58 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

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

Root cause confirmed. The bug is in ClientService.Update (internal/web/service/client_crud.go), which backs POST /panel/api/clients/update/{email} via UpdateByEmail. When the request changes email, the function commits the rename to the clients table before it has rewritten the inbound's own settings JSON to match — and a background job that runs on every install can land in that gap and fabricate a duplicate.

Sequence in Update (internal/web/service/client_crud.go):

// lines 349-364 — email is written to the DB immediately, standalone, not in a transaction
if updated.Email != existing.Email {
    ...
    database.GetDB().Model(&model.ClientRecord{}).
        Where("id = ?", id).
        Update("email", updated.Email)
}

// lines 366-376 — the subId collision check (and error) happen AFTER that write
if updated.SubID != "" {
    ...
    if subCollision > 0 {
        return false, common.NewError("Duplicate subId:", updated.SubID) // line 374
    }
}

// lines 378-412 — only now does it loop over the client's inbounds and call
// UpdateInboundClient, which is what actually rewrites each inbound's
// settings.clients[] JSON so its email matches the new one

For the time between the first block and the third, clients.email already holds the new value while every inbound's stored settings JSON still has the old one.

SyncInbound (internal/web/service/client_link.go:12) is what reconciles an inbound's JSON back into the clients table, and it matches purely by exact email string (lines 35-47). Any JSON client entry whose email it can't find in the clients table is treated as a brand-new client and inserted as one (lines 59-66, 134-141) — seeded from whatever is still in that JSON. clients.email has a unique index (internal/database/model/model.go:797), but it doesn't block this insert, because the old email was just vacated by the rename a moment earlier.

SyncInbound isn't only called from this update path. The built-in traffic-accounting job calls it for every inbound it touches, every 5 seconds (cadenceXrayTraffic = "@every 5s", internal/web/web.go:289, wired up at line 327; the call itself is internal/web/service/inbound_traffic.go:278, inside AddClientTraffic). If that job's tick lands inside the window described above, it reads the still-old JSON, no longer finds a matching clients row for the old email, and inserts the duplicate — still carrying the client's original subId, since that field was never part of this edit. That duplicate then collides on subId with the real, renamed client, so the next update attempt trips the check quoted above and returns Duplicate subId: <subid>\n — an exact match for your reported message (common.NewError formats with fmt.Sprintln, internal/util/common/err.go:19-22). The duplicate was already committed independently of that later call, which is why it persists — and can multiply — even when the error is returned.

On the UI-vs-API difference: the panel's own client editor calls this identical endpoint with an equivalent full-client payload (frontend/src/hooks/useClients.ts:324-328, payload built in frontend/src/pages/clients/ClientFormModal.tsx:492-508) — it is not a separate code path. A human clicking Save once and waiting for the toast is much less likely to land inside that window than scripted or repeated API calls against a panel that is actively passing traffic (so the 5s job has real work to do on that inbound), which lines up with your note that this shows up on production panels.

@MHSanaei the fix likely needs the email rename, the subId collision check, and the per-inbound JSON rewrite in Update to happen atomically (or at minimum, run both collision checks before any write touches clients.email), so a failed or slow update can't leave the DB row and the inbound JSON pointing at different emails.

For cleanup: if the duplicates now show 0 attached inbounds in the client list, pages.clients.delOrphans in the Clients page (POST /panel/api/clients/delOrphans, internal/web/service/client_portable.go:168) removes clients rows with no client_inbounds link, which is what these duplicates become once a later sync rebuilds that inbound's links from its now-correct JSON. Back up /etc/x-ui/x-ui.db first and check the list before running it, since it's a hard delete.

This reply was generated automatically. A maintainer may follow up.

<!-- gh-comment-id:4923128453 --> @github-actions[bot] commented on GitHub (Jul 9, 2026): Root cause confirmed. The bug is in `ClientService.Update` (`internal/web/service/client_crud.go`), which backs `POST /panel/api/clients/update/{email}` via `UpdateByEmail`. When the request changes `email`, the function commits the rename to the `clients` table **before** it has rewritten the inbound's own `settings` JSON to match — and a background job that runs on every install can land in that gap and fabricate a duplicate. Sequence in `Update` (`internal/web/service/client_crud.go`): ```go // lines 349-364 — email is written to the DB immediately, standalone, not in a transaction if updated.Email != existing.Email { ... database.GetDB().Model(&model.ClientRecord{}). Where("id = ?", id). Update("email", updated.Email) } // lines 366-376 — the subId collision check (and error) happen AFTER that write if updated.SubID != "" { ... if subCollision > 0 { return false, common.NewError("Duplicate subId:", updated.SubID) // line 374 } } // lines 378-412 — only now does it loop over the client's inbounds and call // UpdateInboundClient, which is what actually rewrites each inbound's // settings.clients[] JSON so its email matches the new one ``` For the time between the first block and the third, `clients.email` already holds the *new* value while every inbound's stored `settings` JSON still has the *old* one. `SyncInbound` (`internal/web/service/client_link.go:12`) is what reconciles an inbound's JSON back into the `clients` table, and it matches purely by exact email string (lines 35-47). Any JSON client entry whose email it can't find in the `clients` table is treated as a brand-new client and inserted as one (lines 59-66, 134-141) — seeded from whatever is still in that JSON. `clients.email` has a unique index (`internal/database/model/model.go:797`), but it doesn't block this insert, because the *old* email was just vacated by the rename a moment earlier. `SyncInbound` isn't only called from this update path. The built-in traffic-accounting job calls it for every inbound it touches, every 5 seconds (`cadenceXrayTraffic = "@every 5s"`, `internal/web/web.go:289`, wired up at line 327; the call itself is `internal/web/service/inbound_traffic.go:278`, inside `AddClientTraffic`). If that job's tick lands inside the window described above, it reads the still-old JSON, no longer finds a matching `clients` row for the old email, and inserts the duplicate — still carrying the client's original `subId`, since that field was never part of this edit. That duplicate then collides on `subId` with the real, renamed client, so the *next* update attempt trips the check quoted above and returns `Duplicate subId: <subid>\n` — an exact match for your reported message (`common.NewError` formats with `fmt.Sprintln`, `internal/util/common/err.go:19-22`). The duplicate was already committed independently of that later call, which is why it persists — and can multiply — even when the error is returned. On the UI-vs-API difference: the panel's own client editor calls this identical endpoint with an equivalent full-client payload (`frontend/src/hooks/useClients.ts:324-328`, payload built in `frontend/src/pages/clients/ClientFormModal.tsx:492-508`) — it is not a separate code path. A human clicking Save once and waiting for the toast is much less likely to land inside that window than scripted or repeated API calls against a panel that is actively passing traffic (so the 5s job has real work to do on that inbound), which lines up with your note that this shows up on production panels. @MHSanaei the fix likely needs the email rename, the subId collision check, and the per-inbound JSON rewrite in `Update` to happen atomically (or at minimum, run both collision checks before any write touches `clients.email`), so a failed or slow update can't leave the DB row and the inbound JSON pointing at different emails. For cleanup: if the duplicates now show 0 attached inbounds in the client list, `pages.clients.delOrphans` in the Clients page (`POST /panel/api/clients/delOrphans`, `internal/web/service/client_portable.go:168`) removes `clients` rows with no `client_inbounds` link, which is what these duplicates become once a later sync rebuilds that inbound's links from its now-correct JSON. Back up `/etc/x-ui/x-ui.db` first and check the list before running it, since it's a hard delete. *This reply was generated automatically. A maintainer may follow up.*
Author
Owner

@HamedSanaei commented on GitHub (Jul 9, 2026):

Update: The issue also happens from the Web UI

After further testing, I can confirm that this issue is not limited to the API.

The same duplicate-client behavior also happens when editing a client directly from the 3x-ui web panel UI.

Updated behavior

When I edit an existing client from the web UI, for example by changing the client's email or comment:

  1. The original client gets updated.
  2. At the same time, a new duplicate client is created.
  3. The duplicate client contains the old/original client data.
  4. If I try to edit the affected client again, the panel may return a Duplicate subId error.
  5. Even after the Duplicate subId error, another duplicate client may still be created.

So the problem seems to be in the shared client update logic, not only in the API endpoint.

Updated reproduction

The bug can now be reproduced in two ways:

Method 1: Through API

Use:

POST /panel/api/clients/update/{email}

Change fields such as email or comment.

Result: the original client is updated, but a duplicate client is also created.

Method 2: Through Web UI

  1. Open the 3x-ui web panel.
  2. Go to the clients list.
  3. Select an existing client.
  4. Edit simple fields such as email or comment.
  5. Save the changes.
  6. Search for the original email/subId in the clients list.

Result: the original client is updated, but an additional duplicate client is created with the previous/original client data.

Updated expected behavior

Editing a client, either from the API or from the web UI, should only update the selected existing client.

It should not create any new client.

Also, the existing subId should not be treated as a duplicate when it belongs to the same client being edited.

Updated actual behavior

Both the API and the web UI can create duplicate clients during an update operation.

The issue is reproducible when changing fields like:

  • email
  • comment

The duplicate client keeps the old/original data, while the original client is updated.

On the next edit attempt, the panel/API may return:

{
  "success": false,
  "msg": "Something went wrong (Duplicate subId: 7e5djjjh\n)",
  "obj": null
}
<!-- gh-comment-id:4923249087 --> @HamedSanaei commented on GitHub (Jul 9, 2026): ## Update: The issue also happens from the Web UI After further testing, I can confirm that this issue is **not limited to the API**. The same duplicate-client behavior also happens when editing a client directly from the 3x-ui web panel UI. ### Updated behavior When I edit an existing client from the web UI, for example by changing the client's email or comment: 1. The original client gets updated. 2. At the same time, a new duplicate client is created. 3. The duplicate client contains the old/original client data. 4. If I try to edit the affected client again, the panel may return a `Duplicate subId` error. 5. Even after the `Duplicate subId` error, another duplicate client may still be created. So the problem seems to be in the shared client update logic, not only in the API endpoint. ### Updated reproduction The bug can now be reproduced in two ways: #### Method 1: Through API Use: `POST /panel/api/clients/update/{email}` Change fields such as `email` or `comment`. Result: the original client is updated, but a duplicate client is also created. #### Method 2: Through Web UI 1. Open the 3x-ui web panel. 2. Go to the clients list. 3. Select an existing client. 4. Edit simple fields such as `email` or `comment`. 5. Save the changes. 6. Search for the original email/subId in the clients list. Result: the original client is updated, but an additional duplicate client is created with the previous/original client data. ### Updated expected behavior Editing a client, either from the API or from the web UI, should only update the selected existing client. It should not create any new client. Also, the existing `subId` should not be treated as a duplicate when it belongs to the same client being edited. ### Updated actual behavior Both the API and the web UI can create duplicate clients during an update operation. The issue is reproducible when changing fields like: - `email` - `comment` The duplicate client keeps the old/original data, while the original client is updated. On the next edit attempt, the panel/API may return: ```json { "success": false, "msg": "Something went wrong (Duplicate subId: 7e5djjjh\n)", "obj": null }
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#4076
No description provided.