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
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/util/sys"
|
|
)
|
|
|
|
// Each listener can take up to 30 seconds to drain ordinary HTTP traffic.
|
|
const panelRestartTimeout = 75 * time.Second
|
|
|
|
type panelRestartPhase struct {
|
|
name string
|
|
run func(context.Context) error
|
|
}
|
|
|
|
type panelRestartSignals struct {
|
|
shutdown bool
|
|
restartXray bool
|
|
}
|
|
|
|
func runPanelRestart(signals <-chan os.Signal, budget time.Duration, phases ...panelRestartPhase) (panelRestartSignals, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), budget)
|
|
defer cancel()
|
|
var phase atomic.Pointer[panelRestartPhase]
|
|
result := make(chan error, 1)
|
|
go func() {
|
|
for i := range phases {
|
|
if err := ctx.Err(); err != nil {
|
|
result <- err
|
|
return
|
|
}
|
|
step := &phases[i]
|
|
phase.Store(step)
|
|
started := time.Now()
|
|
log.Printf("panel restart phase=%s state=started", step.name)
|
|
err := step.run(ctx)
|
|
log.Printf("panel restart phase=%s state=finished duration=%s error=%v", step.name, time.Since(started), err)
|
|
if err != nil {
|
|
result <- fmt.Errorf("%s: %w", step.name, err)
|
|
return
|
|
}
|
|
}
|
|
result <- ctx.Err()
|
|
}()
|
|
|
|
var pending panelRestartSignals
|
|
phaseName := func() string {
|
|
if current := phase.Load(); current != nil {
|
|
return current.name
|
|
}
|
|
return "dispatch"
|
|
}
|
|
for {
|
|
select {
|
|
case err := <-result:
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
return pending, fmt.Errorf("%s exceeded the %s panel restart deadline: %w", phaseName(), budget, ctx.Err())
|
|
}
|
|
return pending, err
|
|
case <-ctx.Done():
|
|
return pending, fmt.Errorf("%s exceeded the %s panel restart deadline: %w", phaseName(), budget, ctx.Err())
|
|
case sig, ok := <-signals:
|
|
if !ok {
|
|
signals = nil
|
|
continue
|
|
}
|
|
switch sig {
|
|
case syscall.SIGHUP:
|
|
continue
|
|
case sys.SIGUSR1:
|
|
pending.restartXray = true
|
|
default:
|
|
// Let the current restart finish within its existing deadline,
|
|
// then let the signal loop stop the new generation gracefully.
|
|
pending.shutdown = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Log function names only. Argument values and goroutine labels can contain panel data.
|
|
func logPanelRestartStacks() {
|
|
records := make([]runtime.StackRecord, 2048)
|
|
n, ok := runtime.GoroutineProfile(records)
|
|
if !ok {
|
|
log.Printf("panel restart goroutine profile omitted: %d goroutines exceed diagnostic capacity", n)
|
|
return
|
|
}
|
|
log.Printf("panel restart goroutine profile: %d goroutines, showing up to 24 stacks", n)
|
|
for _, record := range records[:min(n, 24)] {
|
|
frames := runtime.CallersFrames(record.Stack())
|
|
names := make([]string, 0, 12)
|
|
for range cap(names) {
|
|
frame, more := frames.Next()
|
|
if frame.Function != "" {
|
|
names = append(names, frame.Function)
|
|
}
|
|
if !more {
|
|
break
|
|
}
|
|
}
|
|
log.Print(strings.Join(names, " <- "))
|
|
}
|
|
}
|