fix(hwid): serialize the device-limit write with its trim (#6591)
setClientLimitHwidByEmail wrote clients.limit_hwid and then trimmed client_hwids as two independent statements. A traffic-cycle Save that read the record before the limit changed could write the stale value back after it, and a failed trim committed the new limit anyway. Both halves now run inside runSerializedTx, the transaction the traffic writer already owns. setClientLimitHwidByEmailTx and clearClientHwidsBySubIDTx refuse a handle that is not that transaction (errClientHwidWriteNotSerialized) instead of falling back to the shared handle. Client delete moves onto the same writer, and BulkCreate withdraws a re-created client's tombstone before applying its optional HWID limit. TestSetClientLimitHwidIsSerializedWithSyncInbound holds a stale traffic-cycle Save open across the limit change and fails without the serialization (limit_hwid = 5, want 1).
This commit is contained in:
1 parent
d86a3def85
commit
c0c0136037
6 files changed
+255
-16
No files matched your search
@@ -553,7 +553,7 @@ func (s *ClientService) BulkAdjust(inboundSvc *InboundService, emails []string,
|
||||
}
|
||||
}
|
||||
if adjustHwid {
|
||||
if err := s.setClientLimitHwidByEmail(db, email, *limitHwid); err != nil {
|
||||
if err := s.setClientLimitHwidByEmail(email, *limitHwid); err != nil {
|
||||
if _, already := skippedReasons[email]; !already {
|
||||
skippedReasons[email] = err.Error()
|
||||
}
|
||||
@@ -1487,22 +1487,22 @@ func (s *ClientService) BulkCreate(inboundSvc *InboundService, payloads []Client
|
||||
}
|
||||
}
|
||||
|
||||
createdEmails := make([]string, 0, len(prep))
|
||||
for idx := range prep {
|
||||
if failed[idx] {
|
||||
skip(prep[idx].client.Email, reason[idx])
|
||||
continue
|
||||
}
|
||||
if err := s.setClientLimitHwidByEmail(nil, prep[idx].client.Email, prep[idx].limitHwid); err != nil {
|
||||
// The client is already live after fanout; never leave a stale delete
|
||||
// tombstone merely because applying its optional HWID limit failed.
|
||||
withdrawClientTombstones(prep[idx].client.Email)
|
||||
if err := s.setClientLimitHwidByEmail(prep[idx].client.Email, prep[idx].limitHwid); err != nil {
|
||||
skip(prep[idx].client.Email, err.Error())
|
||||
continue
|
||||
}
|
||||
createdEmails = append(createdEmails, prep[idx].client.Email)
|
||||
result.Created++
|
||||
}
|
||||
// A re-created email is a live identity again: a delete tombstone left
|
||||
// standing makes the next node merge prune the new client's inbound links.
|
||||
withdrawClientTombstones(createdEmails...)
|
||||
return result, needRestart, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -237,7 +237,7 @@ func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreate
|
||||
// A re-created email is a live identity again: a delete tombstone left
|
||||
// standing makes the next node merge prune the new client's inbound links.
|
||||
withdrawClientTombstones(client.Email)
|
||||
return needRestart, s.setClientLimitHwidByEmail(nil, client.Email, payload.LimitHwid)
|
||||
return needRestart, s.setClientLimitHwidByEmail(client.Email, payload.LimitHwid)
|
||||
}
|
||||
|
||||
// inboundFanoutConcurrency caps how many inbounds one client op applies at
|
||||
@@ -803,7 +803,7 @@ func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model
|
||||
return needRestart, err
|
||||
}
|
||||
|
||||
if err := s.setClientLimitHwidByEmail(nil, updated.Email, limitHwid); err != nil {
|
||||
if err := s.setClientLimitHwidByEmail(updated.Email, limitHwid); err != nil {
|
||||
return needRestart, err
|
||||
}
|
||||
|
||||
@@ -868,8 +868,7 @@ func (s *ClientService) Delete(inboundSvc *InboundService, id int, keepTraffic b
|
||||
return needRestart, errors.Join(delErrs...)
|
||||
}
|
||||
|
||||
db := database.GetDB()
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := runSerializedTx(func(tx *gorm.DB) error {
|
||||
if existing.Email != "" {
|
||||
if err := adjustGroupBaselinesForRemovedTraffic(tx, []string{existing.Email}); err != nil {
|
||||
return err
|
||||
|
||||
@@ -48,6 +48,8 @@ const (
|
||||
hwidFingerprintLength = 12
|
||||
)
|
||||
|
||||
var errClientHwidWriteNotSerialized = errors.New("client HWID write requires the serialized transaction")
|
||||
|
||||
type ClientHwidInfo struct {
|
||||
Id int `json:"id"`
|
||||
FirstSeen int64 `json:"firstSeen"`
|
||||
@@ -300,9 +302,16 @@ func (s *ClientService) DeleteClientHwid(email string, id int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ClientService) setClientLimitHwidByEmail(tx *gorm.DB, email string, limit int) error {
|
||||
if tx == nil {
|
||||
tx = database.GetDB()
|
||||
// Serialize the limit write and trim with SyncInbound and client deletion.
|
||||
func (s *ClientService) setClientLimitHwidByEmail(email string, limit int) error {
|
||||
return runSerializedTx(func(tx *gorm.DB) error {
|
||||
return s.setClientLimitHwidByEmailTx(tx, email, limit)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ClientService) setClientLimitHwidByEmailTx(tx *gorm.DB, email string, limit int) error {
|
||||
if !isSerializedTx(tx) {
|
||||
return errClientHwidWriteNotSerialized
|
||||
}
|
||||
if limit < 0 {
|
||||
limit = 0
|
||||
@@ -346,8 +355,8 @@ func trimClientHwidsForSubID(tx *gorm.DB, subID string, limit int) error {
|
||||
}
|
||||
|
||||
func clearClientHwidsBySubIDTx(tx *gorm.DB, subIDs ...string) error {
|
||||
if tx == nil {
|
||||
tx = database.GetDB()
|
||||
if !isSerializedTx(tx) {
|
||||
return errClientHwidWriteNotSerialized
|
||||
}
|
||||
clean := make([]string, 0, len(subIDs))
|
||||
seen := map[string]struct{}{}
|
||||
|
||||
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user