fix(clients): stop client ops from reverting a renewal committed mid-op

Invariant: an operation on an inbound's clients writes back only what it
changed, onto the settings as committed when it writes. Every client op
(add, edit, delete, bulk adjust/detach/delete/set-enable) read the inbound
outside the serial traffic writer and then tx.Save'd the whole row inside
it. A traffic tick that committed in between - auto-renew re-enabling a
client, the delayed-start conversion, a node adoption - was overwritten
with the stale copy. A renewed neighbour ended up enable=false with its old
expiry in settings while client_traffics said enabled, so the next runtime
rebuild dropped a healthy client nobody had touched. The bulk ops then ran
a full SyncInbound from those stale settings, copying enable=false into the
client record too.

Each site now commits through commitInboundClientSettings: inside the
serialized tx it three-way merges the op's edit (read -> output, per client
and per field) onto the committed settings and updates only the settings
column, which also stops the stale up/down/enable inbound columns being
written back. advancePushedInbound now records what the per-client push
delivered rather than the merged settings, so the node's reconcile-skip
fingerprint cannot claim a renewal it never received.
This commit is contained in:
MHSanaei committed 2026-09-28 01:59:33 +02:00
1 parent 15d82a5e47
commit 63ffc083e4
4 files changed
+497 -16

No files matched your search

+6 -4
View File
@@ -795,6 +795,7 @@ func (s *ClientService) bulkAdjustInboundClients(
}
return res
}
prevSettings := oldInbound.Settings
oldInbound.Settings = string(newSettings)
// A flow change rewrites the user's xray config, which the lightweight
@@ -807,7 +808,7 @@ func (s *ClientService) bulkAdjustInboundClients(
// Serialize against the traffic poll to avoid the cross-transaction
// lock-order deadlock on inbounds/client_records (runSerializedTx).
txErr := runSerializedTx(func(tx *gorm.DB) error {
if err := tx.Save(oldInbound).Error; err != nil {
if err := commitInboundClientSettings(tx, oldInbound, prevSettings); err != nil {
return err
}
finalClients, gcErr := inboundSvc.GetClients(oldInbound)
@@ -1112,6 +1113,7 @@ func (s *ClientService) bulkDelInboundClients(
}
return res
}
prevSettings := oldInbound.Settings
oldInbound.Settings = string(newSettings)
foundList := make([]string, 0, len(foundEmails))
@@ -1181,7 +1183,7 @@ func (s *ClientService) bulkDelInboundClients(
// Serialize against the traffic poll to avoid the cross-transaction
// lock-order deadlock on inbounds/client_records (runSerializedTx).
txErr := runSerializedTx(func(tx *gorm.DB) error {
if err := tx.Save(oldInbound).Error; err != nil {
if err := commitInboundClientSettings(tx, oldInbound, prevSettings); err != nil {
return err
}
finalClients, err := inboundSvc.GetClients(oldInbound)
@@ -1780,7 +1782,7 @@ func (s *ClientService) bulkSetEnableInboundClients(inboundSvc *InboundService,
}
txErr := runSerializedTx(func(tx *gorm.DB) error {
if e := tx.Save(oldInbound).Error; e != nil {
if e := commitInboundClientSettings(tx, oldInbound, prevSettings); e != nil {
return e
}
finalClients, gcErr := inboundSvc.GetClients(oldInbound)
@@ -1850,7 +1852,7 @@ func (s *ClientService) bulkSetEnableInboundClients(inboundSvc *InboundService,
}
}
if !pushFailed {
advancePushedInbound(rt, prevSettings, oldInbound)
advancePushedInbound(rt, prevSettings, string(newSettings), oldInbound)
}
}
+14 -12
View File
@@ -39,16 +39,18 @@ func sameClientConfigExceptUpdatedAt(a, b map[string]any) bool {
return aerr == nil && berr == nil && string(an) == string(bn)
}
// advancePushedInbound advances the node's reconcile-skip fingerprint from the
// pre-edit settings to the saved ones after every per-client push succeeded.
func advancePushedInbound(rt runtime.Runtime, prevSettings string, ib *model.Inbound) {
// advancePushedInbound advances the node's reconcile-skip fingerprint to what the
// per-client pushes delivered, not the saved settings a traffic tick may have extended.
func advancePushedInbound(rt runtime.Runtime, prevSettings, pushedSettings string, ib *model.Inbound) {
rem, ok := rt.(*runtime.Remote)
if !ok {
return
}
prev := *ib
prev.Settings = prevSettings
rem.AdvancePushedInbound(&prev, ib)
pushed := *ib
pushed.Settings = pushedSettings
rem.AdvancePushedInbound(&prev, &pushed)
}
// delInboundClients removes several clients from a single inbound in one pass:
@@ -184,7 +186,7 @@ func (s *ClientService) delInboundClients(inboundSvc *InboundService, inboundId
}
}
}
if e := tx.Save(oldInbound).Error; e != nil {
if e := commitInboundClientSettings(tx, oldInbound, prevSettings); e != nil {
return e
}
detached := make([]string, 0, len(targets))
@@ -249,7 +251,7 @@ func (s *ClientService) delInboundClients(inboundSvc *InboundService, inboundId
}
}
if nodePush && !nodePushFailed {
advancePushedInbound(nodeRt, prevSettings, oldInbound)
advancePushedInbound(nodeRt, prevSettings, string(newSettings), oldInbound)
}
return needRestart, nil
@@ -563,7 +565,7 @@ func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model
return e
}
}
if e := tx.Save(oldInbound).Error; e != nil {
if e := commitInboundClientSettings(tx, oldInbound, prevSettings); e != nil {
return e
}
if err := s.ApplyInboundClientDelta(tx, oldInbound.Id, addedClients, nil); err != nil {
@@ -648,7 +650,7 @@ func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model
}
}
if push {
advancePushedInbound(rt, prevSettings, oldInbound)
advancePushedInbound(rt, prevSettings, string(newSettings), oldInbound)
}
}
@@ -980,7 +982,7 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
}
}
if e := tx.Save(oldInbound).Error; e != nil {
if e := commitInboundClientSettings(tx, oldInbound, prevSettings); e != nil {
return e
}
// Rename the client record in the same transaction as the settings JSON
@@ -1074,7 +1076,7 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
if err1 != nil {
logger.Warning("Error in updating client on", rt.Name(), ":", err1)
} else {
advancePushedInbound(rt, prevSettings, oldInbound)
advancePushedInbound(rt, prevSettings, string(newSettings), oldInbound)
}
}
} else {
@@ -1184,7 +1186,7 @@ func (s *ClientService) DelInboundClientByEmail(inboundSvc *InboundService, inbo
return e
}
}
if e := tx.Save(oldInbound).Error; e != nil {
if e := commitInboundClientSettings(tx, oldInbound, prevSettings); e != nil {
return e
}
if err := s.ApplyInboundClientDelta(tx, inboundId, nil, []string{email}); err != nil {
@@ -1248,7 +1250,7 @@ func (s *ClientService) DelInboundClientByEmail(inboundSvc *InboundService, inbo
if err1 != nil {
logger.Warning("Error in deleting client on", rt.Name(), ":", err1)
} else {
advancePushedInbound(rt, prevSettings, oldInbound)
advancePushedInbound(rt, prevSettings, string(newSettings), oldInbound)
}
}
}
@@ -0,0 +1,141 @@
package service
import (
"encoding/json"
"reflect"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"gorm.io/gorm"
)
// commitInboundClientSettings writes a client op's edit (base → ib.Settings) onto
// the settings committed now: a traffic tick after the op's read must survive.
func commitInboundClientSettings(tx *gorm.DB, ib *model.Inbound, base string) error {
var current []string
if err := tx.Model(&model.Inbound{}).Where("id = ?", ib.Id).Pluck("settings", &current).Error; err != nil {
return err
}
if len(current) == 1 && current[0] != base {
merged, err := rebaseClientSettings(base, ib.Settings, current[0])
if err != nil {
return err
}
ib.Settings = merged
}
return tx.Model(&model.Inbound{}).Where("id = ?", ib.Id).Update("settings", ib.Settings).Error
}
// rebaseClientSettings three-way merges settings JSON: every key and client field
// ours left as base had it takes current's value; clients are matched by email.
func rebaseClientSettings(base, ours, current string) (string, error) {
var baseM, oursM, curM map[string]any
for _, p := range []struct {
raw string
dst *map[string]any
}{{base, &baseM}, {ours, &oursM}, {current, &curM}} {
if err := json.Unmarshal([]byte(p.raw), p.dst); err != nil {
return "", err
}
}
out := mergeFields(baseM, oursM, curM)
baseClients, _ := baseM["clients"].([]any)
oursClients, _ := oursM["clients"].([]any)
curClients, _ := curM["clients"].([]any)
if _, has := oursM["clients"]; has {
out["clients"] = mergeClientLists(baseClients, oursClients, curClients)
}
b, err := json.MarshalIndent(out, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
func mergeFields(base, ours, current map[string]any) map[string]any {
out := make(map[string]any, len(current)+len(ours))
for k, v := range current {
out[k] = v
}
keys := make(map[string]struct{}, len(base)+len(ours))
for k := range base {
keys[k] = struct{}{}
}
for k := range ours {
keys[k] = struct{}{}
}
for k := range keys {
bv, inBase := base[k]
ov, inOurs := ours[k]
if inBase == inOurs && reflect.DeepEqual(bv, ov) {
continue
}
if inOurs {
out[k] = ov
} else {
delete(out, k)
}
}
return out
}
func clientEntryEmail(entry any) (map[string]any, string) {
m, ok := entry.(map[string]any)
if !ok {
return nil, ""
}
email, _ := m["email"].(string)
return m, email
}
func indexClientsByEmail(list []any) map[string]map[string]any {
out := make(map[string]map[string]any, len(list))
for _, entry := range list {
if m, email := clientEntryEmail(entry); email != "" {
out[email] = m
}
}
return out
}
// mergeClientLists keeps ours' order. A client ours removed stays removed; one a
// concurrent writer added or removed keeps that change unless ours edited it.
func mergeClientLists(base, ours, current []any) []any {
baseBy := indexClientsByEmail(base)
curBy := indexClientsByEmail(current)
out := make([]any, 0, len(ours)+len(current))
placed := make(map[string]struct{}, len(ours))
for _, entry := range ours {
o, email := clientEntryEmail(entry)
if email == "" {
out = append(out, entry)
continue
}
placed[email] = struct{}{}
b, inBase := baseBy[email]
c, inCur := curBy[email]
switch {
case !inBase:
out = append(out, o)
case !inCur:
if !reflect.DeepEqual(b, o) {
out = append(out, o)
}
default:
out = append(out, mergeFields(b, o, c))
}
}
for _, entry := range current {
c, email := clientEntryEmail(entry)
if email == "" {
continue
}
if _, done := placed[email]; done {
continue
}
if _, inBase := baseBy[email]; !inBase {
out = append(out, c)
}
}
return out
}
Loaded 3 of 4 files, more files were not shown because too many files have changed in this diff. Show more