mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-04 00:15:09 +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
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
// Package global provides global variables and interfaces for accessing web and subscription servers.
|
|
package global
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
_ "unsafe"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
)
|
|
|
|
var (
|
|
serverMu sync.RWMutex
|
|
webServer WebServer
|
|
subServer SubServer
|
|
|
|
restartHookMu sync.RWMutex
|
|
restartHook func()
|
|
)
|
|
|
|
// WebServer interface defines methods for accessing the web server instance.
|
|
type WebServer interface {
|
|
GetCron() *cron.Cron // Get the cron scheduler
|
|
GetCtx() context.Context // Get the server context
|
|
GetWSHub() any // Get the WebSocket hub (using any to avoid circular dependency)
|
|
}
|
|
|
|
// SubServer interface defines methods for accessing the subscription server instance.
|
|
type SubServer interface {
|
|
GetCtx() context.Context // Get the server context
|
|
}
|
|
|
|
// SetWebServer sets the global web server instance.
|
|
func SetWebServer(s WebServer) {
|
|
serverMu.Lock()
|
|
defer serverMu.Unlock()
|
|
webServer = s
|
|
}
|
|
|
|
// GetWebServer returns the global web server instance.
|
|
func GetWebServer() WebServer {
|
|
serverMu.RLock()
|
|
defer serverMu.RUnlock()
|
|
return webServer
|
|
}
|
|
|
|
// SetSubServer sets the global subscription server instance.
|
|
func SetSubServer(s SubServer) {
|
|
serverMu.Lock()
|
|
defer serverMu.Unlock()
|
|
subServer = s
|
|
}
|
|
|
|
// GetSubServer returns the global subscription server instance.
|
|
func GetSubServer() SubServer {
|
|
serverMu.RLock()
|
|
defer serverMu.RUnlock()
|
|
return subServer
|
|
}
|
|
|
|
// SetRestartHook registers a callback that triggers an in-process panel
|
|
// restart. main.go sets this up to push SIGHUP into its own signal channel
|
|
// so the restart path works on Windows (where p.Signal(SIGHUP) is unsupported).
|
|
func SetRestartHook(fn func()) {
|
|
restartHookMu.Lock()
|
|
defer restartHookMu.Unlock()
|
|
restartHook = fn
|
|
}
|
|
|
|
// TriggerRestart fires the registered restart hook. Returns false if none is set.
|
|
func TriggerRestart() bool {
|
|
restartHookMu.RLock()
|
|
fn := restartHook
|
|
restartHookMu.RUnlock()
|
|
if fn == nil {
|
|
return false
|
|
}
|
|
fn()
|
|
return true
|
|
}
|