fix(xray): guard log-writer race and bound handler gRPC deadlines (#5442)

* perf(xray): compile log/traffic regexps once at package scope

GetTraffic recompiled two stats regexps on every traffic tick, and LogWriter.Write
recompiled two more on every log line. Hoist all four to package-level vars so they
compile once at load instead of per call on hot paths.

* fix(xray): guard LogWriter.lastLine against the GetResult reader race

Write is driven by the Xray process goroutine while Process.GetResult
reads lastLine from the caller's goroutine, so the unsynchronized field
is a data race under `go test -race`. Add an RWMutex and route every
write through setLastLine; GetResult reads via LastLine().

* fix(xray): bound handler gRPC calls with a deadline

AddInbound, DelInbound and the AddUser AlterInbound call used
context.Background(), so a hung core connection could block the caller
indefinitely (for example while the process restart lock is held). Give
them a 10s deadline (handlerRPCTimeout) and a nil-client guard, matching
the other handler operations.
This commit is contained in:
n0ctal
2026-06-20 21:10:18 +05:00
committed by GitHub
parent 3cf3fddf12
commit 2bb29468d8
4 changed files with 84 additions and 10 deletions
+3 -2
View File
@@ -273,10 +273,11 @@ func (p *process) GetResult() string {
p.mu.RLock()
exitErr := p.exitErr
p.mu.RUnlock()
if len(p.logWriter.lastLine) == 0 && exitErr != nil {
lastLine := p.logWriter.LastLine()
if len(lastLine) == 0 && exitErr != nil {
return exitErr.Error()
}
return p.logWriter.lastLine
return lastLine
}
// GetVersion returns the version string of the Xray process.