mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 23:45:11 +03:00
b2d7a9bb08
Fix the production-confirmed ABBA lock inversion introduced in v3.12.0: Xray restart held the lifecycle mutex while synchronously waiting for the serialized traffic writer, which needed the same mutex to amend the committed live configuration snapshot. This blocked API writes and node synchronization, and made panel reload wait indefinitely for cron shutdown. Run traffic maintenance before acquiring the Xray lifecycle mutex, keep config generation free of writer submissions, and retain the live-snapshot generation guard. Bound panel-only reload with one 75-second deadline across shutdown and startup; fence replacement workers until the old generation drains, and exit nonzero for supervised recovery on failure. Preserve Xray on healthy reloads, gracefully handle termination signals, log phase timings and bounded function-only stack diagnostics, and distinguish accepted restart requests from successful completion. Validation: all 13 GitHub checks passed on the reviewed head, including Go tests, race, PostgreSQL durable-first/migration, node and Xray E2E, frontend, lint, codegen, vulnerability check and docs. PR validation reports isolated SQLite/PostgreSQL 18 reload tests with 15,844 clients and 30,270 memberships, stable real Xray PIDs, and legacy/current node interoperability. Addresses the primary deadlock and bounded panel reload; writer cancellation and per-source listener admission diagnostics remain follow-ups. Supervised failure recovery may interrupt Xray sessions. Fixes #7345
80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestServerStopWhenXrayNotRunningSucceeds(t *testing.T) {
|
|
t.Setenv("XUI_DB_FOLDER", t.TempDir())
|
|
s := NewServer(nil)
|
|
if err := s.Stop(); err != nil {
|
|
t.Fatalf("expected nil error when stopping server with unstarted xray, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestServerStopPanelOnlyWithPreclosedListenerSucceeds(t *testing.T) {
|
|
t.Setenv("XUI_DB_FOLDER", t.TempDir())
|
|
s := NewServer(nil)
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("net.Listen failed: %v", err)
|
|
}
|
|
s.listener = ln
|
|
s.httpServer = &http.Server{}
|
|
go func() { _ = s.httpServer.Serve(ln) }()
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
_ = s.httpServer.Shutdown(shutdownCtx)
|
|
|
|
if err := s.StopPanelOnly(context.Background()); err != nil {
|
|
t.Fatalf("expected nil error when listener is already closed, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestServerStopPanelOnlyHonorsCallerDeadline(t *testing.T) {
|
|
t.Setenv("XUI_DB_FOLDER", t.TempDir())
|
|
s := NewServer(nil)
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.listener = ln
|
|
entered, release := make(chan struct{}), make(chan struct{})
|
|
s.httpServer = &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(entered)
|
|
<-release
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})}
|
|
t.Cleanup(func() {
|
|
close(release)
|
|
_ = s.httpServer.Close()
|
|
})
|
|
go func() { _ = s.httpServer.Serve(ln) }()
|
|
requestDone := make(chan error, 1)
|
|
go func() {
|
|
client := &http.Client{Timeout: 3 * time.Second}
|
|
res, err := client.Get("http://" + ln.Addr().String())
|
|
if err == nil {
|
|
_ = res.Body.Close()
|
|
}
|
|
requestDone <- err
|
|
}()
|
|
select {
|
|
case <-entered:
|
|
case err := <-requestDone:
|
|
t.Fatalf("handler did not start: %v", err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
defer cancel()
|
|
if err := s.StopPanelOnly(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("active HTTP drain ignored its caller deadline: %v", err)
|
|
}
|
|
}
|