From 267379456ae3bcedbd148d4b158cb72b792ddfc2 Mon Sep 17 00:00:00 2001 From: Sanaei Date: Mon, 30 Nov 2026 23:17:21 +0330 Subject: [PATCH] chore(wirecodec): pin the node encoder's retained-memory budget Two of the three options that bound the encoder had no test. With the worker cap removed, or zstd's low-memory mode dropped, every test stayed green while an encoder built for 64 CPUs retained 154 MiB or 13.7 MiB instead of 9.1 MiB. Only the 1 MiB window was pinned, through the frame header. Build the encoder in newEncoder(cpus) so a test can size it for a many-core host whatever machine runs it, warm every pooled workspace and fail above 12 MiB. --- internal/util/wirecodec/wirecodec.go | 19 ++++++++++++------ internal/util/wirecodec/wirecodec_test.go | 24 ++++++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/internal/util/wirecodec/wirecodec.go b/internal/util/wirecodec/wirecodec.go index aefe0ef17..5d7df1d70 100644 --- a/internal/util/wirecodec/wirecodec.go +++ b/internal/util/wirecodec/wirecodec.go @@ -49,18 +49,25 @@ const ( // EncodeAll/DecodeAll on these shared instances are safe for concurrent use. var ( - encoderWorkers = min(maxEncoderWorkers, runtime.GOMAXPROCS(0)) - encoderSlots = make(chan struct{}, encoderWorkers) - zstdEncoder, _ = zstd.NewWriter(nil, zstd.WithWindowSize(encoderWindow), - zstd.WithEncoderConcurrency(encoderWorkers), zstd.WithLowerEncoderMem(true)) - decoderWorkers = runtime.GOMAXPROCS(0) - zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes), + zstdEncoder, encoderWorkers = newEncoder(runtime.GOMAXPROCS(0)) + encoderSlots = make(chan struct{}, encoderWorkers) + decoderWorkers = runtime.GOMAXPROCS(0) + zstdDecoder, _ = zstd.NewReader(nil, zstd.WithDecoderMaxMemory(maxDecodeBytes), zstd.WithDecodeAllCapLimit(true), zstd.WithDecoderConcurrency(decoderWorkers)) // decodeSlots keeps callers waiting for a decoder worker from each holding an output buffer. decodeSlots = make(chan struct{}, decoderWorkers) errDecodedTooLarge = errors.New("wirecodec: decompressed body exceeds limit") ) +// newEncoder builds the envelope encoder for a host with cpus CPUs and reports +// how many workspaces it pools; they are never freed, hence the cap (#7274). +func newEncoder(cpus int) (*zstd.Encoder, int) { + workers := min(maxEncoderWorkers, cpus) + enc, _ := zstd.NewWriter(nil, zstd.WithWindowSize(encoderWindow), + zstd.WithEncoderConcurrency(workers), zstd.WithLowerEncoderMem(true)) + return enc, workers +} + // Compress zstd-compresses b. func Compress(b []byte) []byte { out, _ := CompressContext(context.Background(), b) diff --git a/internal/util/wirecodec/wirecodec_test.go b/internal/util/wirecodec/wirecodec_test.go index bb01f9b10..a34f0fd3f 100644 --- a/internal/util/wirecodec/wirecodec_test.go +++ b/internal/util/wirecodec/wirecodec_test.go @@ -72,6 +72,28 @@ func TestDecompressOfManyFramesAllocatesTheOutputOnce(t *testing.T) { } } +// The encoder keeps every workspace it has used for the life of the process, so +// their total must not grow with the master's CPU count (#7274). +func TestEncoderWorkspacesStayWithinBudgetOnAManyCoreHost(t *testing.T) { + const cpus, budget = 64, 12 << 20 + payload := bytes.Repeat([]byte("synthetic client traffic row "), 20_000) + enc, _ := newEncoder(cpus) + defer enc.Close() + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + // EncodeAll hands the workspaces out in rotation, so sequential calls warm each one. + for range 2 * cpus { + enc.EncodeAll(payload, nil) + } + runtime.GC() + runtime.ReadMemStats(&after) + if retained := int64(after.HeapAlloc) - int64(before.HeapAlloc); retained > budget { + t.Fatalf("encoder for %d CPUs retains %.1f MiB after use, want at most %d MiB", + cpus, float64(retained)/(1<<20), budget>>20) + } +} + func TestCompressBoundsFrameWindow(t *testing.T) { orig := bytes.Repeat([]byte("synthetic node configuration"), 150_000) packed := Compress(orig) @@ -97,7 +119,7 @@ func TestCompressConcurrentFramesAreCompatible(t *testing.T) { var callers sync.WaitGroup for i := range 16 { callers.Go(func() { - raw := bytes.Repeat([]byte(fmt.Sprintf("client-%d", i)), 40_000) + raw := bytes.Repeat(fmt.Appendf(nil, "client-%d", i), 40_000) packed, err := CompressContext(t.Context(), raw) if err != nil { t.Errorf("encode %d: %v", i, err)