Compare commits

..
1 Commits
Author SHA1 Message Date
Fangliding 6d3ebc4239 Print trace if close take too long 2026-05-19 22:18:26 +08:00
2 changed files with 67 additions and 29 deletions
+45 -28
View File
@@ -3,7 +3,11 @@ package signal
import ( import (
"context" "context"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/task"
) )
type ActivityUpdater interface { type ActivityUpdater interface {
@@ -11,35 +15,45 @@ type ActivityUpdater interface {
} }
type ActivityTimer struct { type ActivityTimer struct {
mu sync.Mutex mu sync.RWMutex
// timer will be nil if this timer is already finished updated chan struct{}
timer *time.Timer checkTask *task.Periodic
timeout time.Duration
onTimeout func() onTimeout func()
consumed atomic.Bool
once sync.Once
} }
func (t *ActivityTimer) Update() { func (t *ActivityTimer) Update() {
// someone already called Update or closing, just return select {
if !t.mu.TryLock() { case t.updated <- struct{}{}:
return default:
} }
defer t.mu.Unlock() }
if t.timer != nil {
t.timer.Reset(t.timeout) func (t *ActivityTimer) check() error {
select {
case <-t.updated:
default:
t.finish()
} }
return nil
} }
func (t *ActivityTimer) finish() { func (t *ActivityTimer) finish() {
t.mu.Lock() t.once.Do(func() {
defer t.mu.Unlock() t.consumed.Store(true)
if t.timer != nil { t.mu.Lock()
t.timer.Stop() defer t.mu.Unlock()
common.CloseIfExists(t.checkTask)
t.onTimeout() t.onTimeout()
t.timer = nil })
}
} }
func (t *ActivityTimer) SetTimeout(timeout time.Duration) { func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
if t.consumed.Load() {
return
}
if timeout == 0 { if timeout == 0 {
t.finish() t.finish()
return return
@@ -47,22 +61,25 @@ func (t *ActivityTimer) SetTimeout(timeout time.Duration) {
t.mu.Lock() t.mu.Lock()
defer t.mu.Unlock() defer t.mu.Unlock()
if t.timer != nil { // double check, just in case
t.timeout = timeout if t.consumed.Load() {
t.timer.Reset(timeout) return
} }
newCheckTask := &task.Periodic{
Interval: timeout,
Execute: t.check,
}
common.CloseIfExists(t.checkTask)
t.checkTask = newCheckTask
t.Update()
common.Must(newCheckTask.Start())
} }
func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer { func CancelAfterInactivity(ctx context.Context, cancel context.CancelFunc, timeout time.Duration) *ActivityTimer {
activityTimer := &ActivityTimer{ timer := &ActivityTimer{
timeout: timeout, updated: make(chan struct{}, 1),
onTimeout: cancel, onTimeout: cancel,
} }
// strange situation timer.SetTimeout(timeout)
if timeout == 0 { return timer
cancel()
return activityTimer
}
activityTimer.timer = time.AfterFunc(timeout, activityTimer.finish)
return activityTimer
} }
+22 -1
View File
@@ -91,7 +91,28 @@ func executeRun(cmd *base.Command, args []string) {
fmt.Println("Failed to start:", err) fmt.Println("Failed to start:", err)
os.Exit(-1) os.Exit(-1)
} }
defer server.Close() defer func() {
closeErrCh := make(chan error, 1)
go func() {
closeErrCh <- server.Close()
}()
select {
case err := <-closeErrCh:
if err != nil {
fmt.Println("Failed to close server:", err)
}
case <-time.After(10 * time.Second):
fmt.Println("Timeout when closing, printing traces:")
buf := make([]byte, 1<<20)
n := runtime.Stack(buf, true)
blocks := strings.Split(string(buf[:n]), "\n\n")
for _, block := range blocks {
if strings.Contains(block, "github.com/xtls/xray-core/core.(*Instance).Close") {
fmt.Println(block)
}
}
}
}()
// Explicitly triggering GC to remove garbage from config loading. // Explicitly triggering GC to remove garbage from config loading.
runtime.GC() runtime.GC()