Skip to content

Commit ebe74a5

Browse files
committed
Fix oom draft flood
1 parent 2364301 commit ebe74a5

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

service/oomkiller/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Service struct {
3232
adaptiveTimer *adaptiveTimer
3333
lastReportTime atomic.Int64
3434
//nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft.
35+
lastDraftTime atomic.Int64
36+
//nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft.
3537
draftCancelled atomic.Bool
3638
}
3739

service/oomkiller/service_darwin.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ import "C"
3434

3535
import (
3636
"sync"
37+
"time"
3738

3839
"github.com/sagernet/sing-box/adapter"
3940
"github.com/sagernet/sing/common/byteformats"
4041
E "github.com/sagernet/sing/common/exceptions"
4142
"github.com/sagernet/sing/service"
4243
)
4344

45+
const oomDraftMinInterval = time.Hour
46+
4447
var (
4548
globalAccess sync.Mutex
4649
globalServices []*Service
@@ -109,6 +112,14 @@ func (s *Service) writeOOMDraft(memoryUsage uint64) {
109112
if s.draftCancelled.Load() {
110113
return
111114
}
115+
now := time.Now().UnixNano()
116+
lastDraft := s.lastDraftTime.Load()
117+
if time.Duration(now-lastDraft) < oomDraftMinInterval {
118+
return
119+
}
120+
if !s.lastDraftTime.CompareAndSwap(lastDraft, now) {
121+
return
122+
}
112123
reporter := service.FromContext[OOMReporter](s.ctx)
113124
if reporter == nil {
114125
return

0 commit comments

Comments
 (0)