mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6591] [MERGED] fix(hwid): serialize the device-limit write with its trim #6365
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#6365
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?
📋 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)
37ffbc0fix(hwid): make the device-limit write and its trim one transaction465027afix(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
setClientLimitHwidByEmailupdatedclient_records.limit_hwidand thendeleted the surplus rows from
client_hwidsunder a handle of its own. Thetraffic cycle reaches the same client rows through
SyncInbound, and aSavethat 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
setClientLimitHwidByEmailwraps its work inrunSerializedTx; the bodymoves to
setClientLimitHwidByEmailTx.setClientLimitHwidByEmailTxrefuses a handle that is not the serializedtransaction (
errClientHwidWriteNotSerialized) rather than silently doingthe unsafe thing.
their own serialized work.
traffic_writer.goexposes the predicate the guard needs.Validation
go build ./...andgo vet ./internal/web/service/are clean.TestSetClientLimitHwidIsSerializedWithSyncInbound,TestClientHwidTxRejectsUnserializedHandle,TestSetClientLimitHwidRollsBackFailedTrim,TestBulkAdjustHwidRollsBackFailedTrim,TestBulkCreateWithdrawsTombstoneWhenHwidTrimFails.TestSetClientLimitHwidIsSerializedWithSyncInboundis the guard: it holds astale
Savefrom the traffic cycle open, runs the limit change, thenreleases. 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/serviceshares one database across the whole test binary anddoes 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,TestClientHwidGateRegistersAndBlocksandTestDeleteClientHwidfail onPostgreSQL on an unmodified checkout of
1c0ce80eas well. I verified thatseparately 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.