mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-06 13:14:11 +00:00
fix(xray): rotate crash reports into log folder, prevent overwrites
writeCrashReport had two flaws: it wrote to the bin folder (alongside the xray binary) which conflates artifacts, and the second-precision timestamp meant a tight restart-loop crash burst overwrote prior reports. Write to the log folder with nanosecond precision and keep the last 10 reports. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
fa3c7a7a0e
commit
74c6d5606b
1 changed files with 39 additions and 3 deletions
|
|
@ -7,7 +7,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
@ -453,8 +455,42 @@ func (p *process) waitForExit(timeout time.Duration) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeCrashReport writes a crash report to the binary folder with a timestamped filename.
|
const (
|
||||||
|
crashReportPrefix = "core_crash_"
|
||||||
|
crashReportSuffix = ".log"
|
||||||
|
maxCrashReports = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
// writeCrashReport persists a captured xray crash chunk to the log folder
|
||||||
|
// with nanosecond-precision filename so restart-loop bursts don't overwrite
|
||||||
|
// each other, and prunes old reports to keep the folder bounded.
|
||||||
func writeCrashReport(m []byte) error {
|
func writeCrashReport(m []byte) error {
|
||||||
crashReportPath := config.GetBinFolderPath() + "/core_crash_" + time.Now().Format("20060102_150405") + ".log"
|
dir := config.GetLogFolder()
|
||||||
return os.WriteFile(crashReportPath, m, 0644)
|
if err := os.MkdirAll(dir, 0o770); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pruneOldCrashReports(dir, maxCrashReports-1)
|
||||||
|
name := crashReportPrefix + time.Now().Format("20060102_150405_000000000") + crashReportSuffix
|
||||||
|
return os.WriteFile(filepath.Join(dir, name), m, 0o640)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pruneOldCrashReports(dir string, keep int) {
|
||||||
|
entries, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var reports []string
|
||||||
|
for _, e := range entries {
|
||||||
|
n := e.Name()
|
||||||
|
if !e.IsDir() && strings.HasPrefix(n, crashReportPrefix) && strings.HasSuffix(n, crashReportSuffix) {
|
||||||
|
reports = append(reports, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(reports) <= keep {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sort.Strings(reports)
|
||||||
|
for _, old := range reports[:len(reports)-keep] {
|
||||||
|
_ = os.Remove(filepath.Join(dir, old))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue