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 authored and GitHub committed 2026-09-18 13:25:24 +03:00 Unverified
1 parent 1c0ce80e8e
commit 95f19b192f
6 files changed
+237 -29

No files matched your search

+1 -1
View File
@@ -106,7 +106,7 @@ func (a *ServerController) startTask() {
}
// status returns the current server status information.
func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.serverService.LastStatus(), nil) }
func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.serverService.CurrentStatus(), nil) }
func (a *ServerController) getFail2banStatus(c *gin.Context) {
jsonObj(c, a.serverService.GetFail2banStatus(), nil)
+7 -1
View File
@@ -1323,10 +1323,16 @@ func (s *NodeService) probe(ctx context.Context, n *model.Node, proxyURL string)
patch.LastError = "decode response: " + err.Error()
return patch, err
}
if !envelope.Success || envelope.Obj == nil {
if !envelope.Success {
patch.LastError = "remote returned success=false: " + envelope.Msg
return patch, errors.New(patch.LastError)
}
// A panel that has not sampled its status yet answers success with a null
// obj; saying so beats "success=false: " with nothing after the colon.
if envelope.Obj == nil {
patch.LastError = "remote panel reported no status yet; it may still be starting up"
return patch, errors.New(patch.LastError)
}
o := envelope.Obj
patch.CpuPct = o.CpuPct
if o.Mem.Total > 0 {
@@ -0,0 +1,48 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
// An older node answers success with a null obj while its status is unsampled,
// which used to surface as "success=false: " with nothing after the colon.
func TestProbeNamesANodeThatHasNoStatusYet(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"success":true,"msg":"","obj":null}`))
}))
defer srv.Close()
u, err := url.Parse(srv.URL)
if err != nil {
t.Fatalf("parse url: %v", err)
}
port, err := strconv.Atoi(u.Port())
if err != nil {
t.Fatalf("parse port: %v", err)
}
n := &model.Node{
Id: 1, Name: "cold", Scheme: "http", Address: u.Hostname(), Port: port,
BasePath: "/", Enable: true, AllowPrivateAddress: true, TlsVerifyMode: "skip",
}
svc := &NodeService{}
patch, err := svc.Probe(context.Background(), n)
if err == nil {
t.Fatal("Probe accepted a status response with no obj, want an error")
}
if strings.Contains(patch.LastError, "success=false") {
t.Fatalf("LastError = %q, want the missing status named instead of a bare success=false", patch.LastError)
}
if !strings.Contains(patch.LastError, "no status yet") {
t.Fatalf("LastError = %q, want it to say the remote reported no status yet", patch.LastError)
}
}
Loaded 3 of 6 files, more files were not shown because too many files have changed in this diff. Show more