From 3f2a2195a8f0d94a9a86229824bfed5f53b3bb13 Mon Sep 17 00:00:00 2001 From: Sanaei Date: Wed, 25 Nov 2026 00:13:21 +0330 Subject: [PATCH] fix(wireguard): apply a peer change pushed to a node at once The previous commit applied a device write at once, but only on the panel that received the request. A node takes the same change as an inbound update from its master, and that path still flagged the 30 s reconcile: a device disabled on the master kept its tunnel on the node until the node's next tick. ReconcileNow is the one place both paths go through. It applies when the core runs and only flags when it is down, so a device write never starts a core the tick has not started. Measured with a real master and node panel (Xray 26.11.9, kernel WireGuard clients): a device disabled on the master is cut on the node after 5 s instead of 11-30 s, and a new one connects 7 s after it is added. No unit test reaches it, a test process is never running; the scenario is the proof. --- internal/web/controller/client-wireguard-devices.go | 10 +++------- internal/web/service/inbound.go | 5 +++-- internal/web/service/xray.go | 9 +++++++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/web/controller/client-wireguard-devices.go b/internal/web/controller/client-wireguard-devices.go index b350d5550..b6abf3d36 100644 --- a/internal/web/controller/client-wireguard-devices.go +++ b/internal/web/controller/client-wireguard-devices.go @@ -3,7 +3,6 @@ package controller import ( "github.com/gin-gonic/gin" - "github.com/mhsanaei/3x-ui/v3/internal/logger" "github.com/mhsanaei/3x-ui/v3/internal/web/service" ) @@ -17,12 +16,9 @@ func (a *ClientController) finishWireguardDeviceWrite(c *gin.Context, err error) jsonObj(c, nil, err) return } - // Applied now, not at the next 30 s reconcile tick: until then a disabled or - // deleted device kept its tunnel (measured 12-24 s on a running panel). - if applyErr := a.xrayService.RestartXray(false); applyErr != nil { - logger.Warning("apply WireGuard device change failed, left to the next reconcile:", applyErr) - a.xrayService.SetToNeedReconcile() - } + // Not left to the 30 s reconcile tick: until then a disabled or deleted device + // kept its tunnel (measured 12-24 s on a running panel). + a.xrayService.ReconcileNow() notifyClientsChanged() a.wireguardDevices(c) } diff --git a/internal/web/service/inbound.go b/internal/web/service/inbound.go index 159fe8881..f8bb2496f 100644 --- a/internal/web/service/inbound.go +++ b/internal/web/service/inbound.go @@ -2433,8 +2433,9 @@ func applyInboundUserOps(rt runtime.Runtime, running, next *model.Inbound) bool } if next.Protocol == model.WireGuard { if !diff.Empty() { - // Reconcile peers and the Xray config snapshot together. - (&XrayService{}).SetToNeedReconcile() + // Peers and the config snapshot move together, and now: a peer removed on a + // node kept its tunnel until the node's next reconcile tick. + (&XrayService{}).ReconcileNow() } return true } diff --git a/internal/web/service/xray.go b/internal/web/service/xray.go index 7327679e3..130272eac 100644 --- a/internal/web/service/xray.go +++ b/internal/web/service/xray.go @@ -1842,6 +1842,15 @@ func (s *XrayService) SetToNeedRestart() { // SetToNeedReconcile schedules config reconciliation without forcing an unchanged core to restart. func (s *XrayService) SetToNeedReconcile() { isNeedXrayReconcile.Store(true) } +// ReconcileNow applies a change at once instead of at the 30 s tick. A core that is +// down only stays flagged: starting it is the tick's job. +func (s *XrayService) ReconcileNow() { + s.SetToNeedReconcile() + if s.IsXrayRunning() { + s.ApplyPendingRestart() + } +} + // GetXrayAPIPort returns the port the local xray process is listening on // for its gRPC HandlerService, or 0 when xray isn't currently running. // Exposed for the runtime package's LocalRuntime adapter without a