From ebe74a5648e7447994761ba8224040d1f69372dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 23 Jun 2026 15:15:01 +0800 Subject: [PATCH] Fix oom draft flood --- service/oomkiller/service.go | 2 ++ service/oomkiller/service_darwin.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/service/oomkiller/service.go b/service/oomkiller/service.go index bbf032d0f..01e078b8a 100644 --- a/service/oomkiller/service.go +++ b/service/oomkiller/service.go @@ -32,6 +32,8 @@ type Service struct { adaptiveTimer *adaptiveTimer lastReportTime atomic.Int64 //nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft. + lastDraftTime atomic.Int64 + //nolint:unused // touched only on darwin && cgo via writeOOMDraft/discardOOMDraft. draftCancelled atomic.Bool } diff --git a/service/oomkiller/service_darwin.go b/service/oomkiller/service_darwin.go index f1d228369..64504ac46 100644 --- a/service/oomkiller/service_darwin.go +++ b/service/oomkiller/service_darwin.go @@ -34,6 +34,7 @@ import "C" import ( "sync" + "time" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing/common/byteformats" @@ -41,6 +42,8 @@ import ( "github.com/sagernet/sing/service" ) +const oomDraftMinInterval = time.Hour + var ( globalAccess sync.Mutex globalServices []*Service @@ -109,6 +112,14 @@ func (s *Service) writeOOMDraft(memoryUsage uint64) { if s.draftCancelled.Load() { return } + now := time.Now().UnixNano() + lastDraft := s.lastDraftTime.Load() + if time.Duration(now-lastDraft) < oomDraftMinInterval { + return + } + if !s.lastDraftTime.CompareAndSwap(lastDraft, now) { + return + } reporter := service.FromContext[OOMReporter](s.ctx) if reporter == nil { return