[PR #6591] [MERGED] fix(hwid): serialize the device-limit write with its trim #6365

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6591
Author: @n0ctal
Created: 9/17/2026
Status: ✅ Merged
Merged: 9/26/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-hwid-limit-transaction


📝 Commits (2)

  • 37ffbc0 fix(hwid): make the device-limit write and its trim one transaction
  • 465027a fix(hwid): serialize limit changes with client lifecycle

📊 Changes

6 files changed (+255 additions, -16 deletions)

View changed files

📝 internal/web/service/client_bulk.go (+5 -5)
📝 internal/web/service/client_crud.go (+3 -4)
📝 internal/web/service/client_hwid.go (+14 -5)
📝 internal/web/service/client_hwid_test.go (+1 -1)
➕ internal/web/service/client_hwid_tx_test.go (+218 -0)
📝 internal/web/service/traffic_writer.go (+14 -1)

📄 Description

Summary

The device-limit write and the HWID trim that enforces it ran as two
independent statements, so a concurrent client write could land between them
and leave the client with a limit the trim never applied.

Why

setClientLimitHwidByEmail updated client_records.limit_hwid and then
deleted the surplus rows from client_hwids under a handle of its own. The
traffic cycle reaches the same client rows through SyncInbound, and a Save
that read the record before the limit changed writes the stale value back
after it — the new limit is lost, and the trim that already ran leaves the
device list shorter than the limit the client ends up with.

Both halves now run inside the serialized transaction the traffic writer
already owns, so the limit change and its trim are one unit against every
other writer of those tables.

Scope

  • setClientLimitHwidByEmail wraps its work in runSerializedTx; the body
    moves to setClientLimitHwidByEmailTx.
  • setClientLimitHwidByEmailTx refuses a handle that is not the serialized
    transaction (errClientHwidWriteNotSerialized) rather than silently doing
    the unsafe thing.
  • The bulk-adjust and create paths call the transactional form from inside
    their own serialized work.
  • traffic_writer.go exposes the predicate the guard needs.

Validation

  • go build ./... and go vet ./internal/web/service/ are clean.
  • Against a real PostgreSQL 16, the five tests this change adds pass:
    TestSetClientLimitHwidIsSerializedWithSyncInbound,
    TestClientHwidTxRejectsUnserializedHandle,
    TestSetClientLimitHwidRollsBackFailedTrim,
    TestBulkAdjustHwidRollsBackFailedTrim,
    TestBulkCreateWithdrawsTombstoneWhenHwidTrimFails.
  • TestSetClientLimitHwidIsSerializedWithSyncInbound is the guard: it holds a
    stale Save from the traffic cycle open, runs the limit change, then
    releases. Removing the serialization makes it fail with
    limit_hwid = 5, want 1.

Two notes on running these against PostgreSQL, neither caused by this change:

  • internal/web/service shares one database across the whole test binary and
    does not clean up between tests, so a second run against the same database
    reports failures that a fresh database does not. Every result above is from
    a pristine database.
  • TestCreateLeavesHwidLimitAloneWhenCreateFails,
    TestClientHwidGateRegistersAndBlocks and TestDeleteClientHwid fail on
    PostgreSQL on an unmodified checkout of 1c0ce80e as well. I verified that
    separately before opening this.

Risk

Low. The change moves existing work under a lock the same code path already
takes elsewhere; the guard turns a silent unsafe write into an error.


🔄 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/6591 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 9/17/2026 **Status:** ✅ Merged **Merged:** 9/26/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-hwid-limit-transaction` --- ### 📝 Commits (2) - [`37ffbc0`](https://github.com/MHSanaei/3x-ui/commit/37ffbc0bbb47f02657a0ebeca09ba8dca9c9f212) fix(hwid): make the device-limit write and its trim one transaction - [`465027a`](https://github.com/MHSanaei/3x-ui/commit/465027ae3e0387d5cfa3eca8417fffeba948eadb) fix(hwid): serialize limit changes with client lifecycle ### 📊 Changes **6 files changed** (+255 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/client_bulk.go` (+5 -5) 📝 `internal/web/service/client_crud.go` (+3 -4) 📝 `internal/web/service/client_hwid.go` (+14 -5) 📝 `internal/web/service/client_hwid_test.go` (+1 -1) ➕ `internal/web/service/client_hwid_tx_test.go` (+218 -0) 📝 `internal/web/service/traffic_writer.go` (+14 -1) </details> ### 📄 Description ## Summary The device-limit write and the HWID trim that enforces it ran as two independent statements, so a concurrent client write could land between them and leave the client with a limit the trim never applied. ## Why `setClientLimitHwidByEmail` updated `client_records.limit_hwid` and then deleted the surplus rows from `client_hwids` under a handle of its own. The traffic cycle reaches the same client rows through `SyncInbound`, and a `Save` that read the record before the limit changed writes the stale value back after it — the new limit is lost, and the trim that already ran leaves the device list shorter than the limit the client ends up with. Both halves now run inside the serialized transaction the traffic writer already owns, so the limit change and its trim are one unit against every other writer of those tables. ## Scope - `setClientLimitHwidByEmail` wraps its work in `runSerializedTx`; the body moves to `setClientLimitHwidByEmailTx`. - `setClientLimitHwidByEmailTx` refuses a handle that is not the serialized transaction (`errClientHwidWriteNotSerialized`) rather than silently doing the unsafe thing. - The bulk-adjust and create paths call the transactional form from inside their own serialized work. - `traffic_writer.go` exposes the predicate the guard needs. ## Validation - `go build ./...` and `go vet ./internal/web/service/` are clean. - Against a real PostgreSQL 16, the five tests this change adds pass: `TestSetClientLimitHwidIsSerializedWithSyncInbound`, `TestClientHwidTxRejectsUnserializedHandle`, `TestSetClientLimitHwidRollsBackFailedTrim`, `TestBulkAdjustHwidRollsBackFailedTrim`, `TestBulkCreateWithdrawsTombstoneWhenHwidTrimFails`. - `TestSetClientLimitHwidIsSerializedWithSyncInbound` is the guard: it holds a stale `Save` from the traffic cycle open, runs the limit change, then releases. Removing the serialization makes it fail with `limit_hwid = 5, want 1`. Two notes on running these against PostgreSQL, neither caused by this change: - `internal/web/service` shares one database across the whole test binary and does not clean up between tests, so a second run against the same database reports failures that a fresh database does not. Every result above is from a pristine database. - `TestCreateLeavesHwidLimitAloneWhenCreateFails`, `TestClientHwidGateRegistersAndBlocks` and `TestDeleteClientHwid` fail on PostgreSQL on an unmodified checkout of `1c0ce80e` as well. I verified that separately before opening this. ## Risk Low. The change moves existing work under a lock the same code path already takes elsewhere; the guard turns a silent unsafe write into an error. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:15 +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#6365
No description provided.