From 99f20dda88979e54b423e6c34b230924795095bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 26 Nov 2026 08:36:00 +0800 Subject: [PATCH] Fix oom-killer JSON cache cleanup --- service/oomkiller/badcleanup.go | 12 ++++++++++-- service/oomkiller/badcleanup_jsonv2.go | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 service/oomkiller/badcleanup_jsonv2.go diff --git a/service/oomkiller/badcleanup.go b/service/oomkiller/badcleanup.go index 4ba4b0ce..3b42e758 100644 --- a/service/oomkiller/badcleanup.go +++ b/service/oomkiller/badcleanup.go @@ -1,4 +1,4 @@ -//go:build badlinkname +//go:build badlinkname && !goexperiment.jsonv2 package oomkiller @@ -7,13 +7,21 @@ import ( _ "unsafe" ) -//go:linkname jsonFieldCache json.fieldCache +//go:linkname jsonEncoderCache encoding/json.encoderCache +var jsonEncoderCache sync.Map + +//go:linkname jsonFieldCache encoding/json.fieldCache var jsonFieldCache sync.Map +//go:linkname contextJSONEncoderCache github.com/sagernet/sing/common/json/internal/contextjson.encoderCache +var contextJSONEncoderCache sync.Map + //go:linkname contextJSONFieldCache github.com/sagernet/sing/common/json/internal/contextjson.fieldCache var contextJSONFieldCache sync.Map func badCleanup() { + jsonEncoderCache.Clear() jsonFieldCache.Clear() + contextJSONEncoderCache.Clear() contextJSONFieldCache.Clear() } diff --git a/service/oomkiller/badcleanup_jsonv2.go b/service/oomkiller/badcleanup_jsonv2.go new file mode 100644 index 00000000..16d8b10a --- /dev/null +++ b/service/oomkiller/badcleanup_jsonv2.go @@ -0,0 +1,23 @@ +//go:build badlinkname && goexperiment.jsonv2 + +package oomkiller + +import ( + "sync" + _ "unsafe" +) + +//go:linkname jsonArshalerCache encoding/json/v2.lookupArshalerCache +var jsonArshalerCache sync.Map + +//go:linkname contextJSONEncoderCache github.com/sagernet/sing/common/json/internal/contextjson.encoderCache +var contextJSONEncoderCache sync.Map + +//go:linkname contextJSONFieldCache github.com/sagernet/sing/common/json/internal/contextjson.fieldCache +var contextJSONFieldCache sync.Map + +func badCleanup() { + jsonArshalerCache.Clear() + contextJSONEncoderCache.Clear() + contextJSONFieldCache.Clear() +}