fix(nodes): stop a restarting panel from reporting itself as down

Adding a node fails right after that node's panel restarts. nodes/add
probes the node's /panel/api/server/status first, and that endpoint
returns whatever the @2s ticker last sampled - nil until the first tick
lands, so the master reads a healthy panel as unreachable and rejects it
with "Add node (remote returned success=false: )", an error whose
message is empty because the node answered success with a null obj.

The window is far wider than one tick: GetStatus resolved the public
IPv4/IPv6 addresses inline and held s.mu across every lookup, so a box
with no IPv6 route spent 3s per service - about 15s of nil status after
each restart, and the same stall on a fresh panel's first sample.

- status now answers from CurrentStatus, which samples on demand when
  the ticker has not run yet instead of returning a null obj
- the public-IP lookups run in the background and outside s.mu, so a
  status sample never waits on them
- probe tells "no status yet" apart from a genuine success=false, so the
  master's error says something when it meets an older node
This commit is contained in:
Farhan Zare
2026-09-18 06:25:24 -04:00
committed by GitHub
parent 1c0ce80e8e
commit 95f19b192f
6 changed files with 237 additions and 29 deletions
@@ -0,0 +1,35 @@
package service
import (
"path/filepath"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
)
// A panel restarts with an empty snapshot until the @2s ticker fires, and a
// master probing that window reads the empty answer as an offline node.
func TestCurrentStatusSamplesBeforeFirstTick(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
svc := &ServerService{}
if svc.LastStatus() != nil {
t.Fatal("a fresh ServerService should hold no snapshot yet")
}
status := svc.CurrentStatus()
if status == nil {
t.Fatal("CurrentStatus returned nil before the first ticker run, want an on-demand sample")
}
if svc.LastStatus() != status {
t.Fatal("the on-demand sample should be stored as LastStatus")
}
if again := svc.CurrentStatus(); again != status {
t.Fatal("a warm CurrentStatus should reuse the stored snapshot, not resample")
}
}