This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: go-1-24-weak-pointer-deterministic-cache-eviction

Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction

Governs the use of Go 1.24 weak pointers (weak.Pointer) and runtime.AddCleanup within high-throughput Go Chi in-memory cache topologies. Replaces legacy runtime.SetFinalizer patterns and explicit stop-the-world eviction sweeps to eliminate GC pacing spikes, finalizer thread starvation, and cyclic reference memory leaks under extreme HTTP request concurrency.

Prohibited Anti-Pattern: Legacy runtime.SetFinalizer and Strong Reference Maps Inducing GC Latency Spikes and Memory Leaks

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction - Governs the use of Go 1.24 weak pointers (weak.Pointer) and runtime.AddCleanup within high-throughput Go Chi in-memory cache topologies. Replaces legacy runtime.SetFinalizer patterns and explicit stop-the-world eviction sweeps to eliminate GC pacing spikes, finalizer thread starvation, and cyclic reference memory leaks under extreme HTTP request concurrency.
globs: **/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/go-1-24-weak-pointer-deterministic-cache-eviction"
---

# Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Legacy runtime.SetFinalizer and Strong Reference Maps Inducing GC Latency Spikes and Memory Leaks

## System Constraints
You are a Principal Systems Engineer enforcing strict Go 1.24 memory management invariants. Ensure that in-memory cache implementations leverage weak pointers (weak.Make) and runtime.AddCleanup instead of runtime.SetFinalizer, manual background sweep storms, or external experimental arenas. Mandate decoupled cleanup closures that never capture target object references, preventing cyclic retention and GC thrashing under high request concurrency.

## Strict Guidelines
- Engineers MUST ALWAYS use Go 1.24 `weak.Pointer[T]` via `weak.Make(ptr)` when storing cached payload references in internal cache metadata nodes.
- Engineers MUST ALWAYS pair weak pointers with `runtime.AddCleanup(ptr, cleanupFunc, arg)` to schedule side-effect cleanups (such as index removals or off-heap releases) without delaying object sweep phases.
- Engineers MUST NOT pass closures to `runtime.AddCleanup` that capture the target object `ptr` either directly or transitively, as this immediately creates a strong cycle and prevents garbage collection.
- Engineers MUST NEVER use `runtime.SetFinalizer`, which pins entire object subgraphs across multiple GC cycles, risks resurrection, and serializes destruction on a single finalizer goroutine.
- Code MUST explicitly validate pointer dereference using `.Value()` on `weak.Pointer[T]` and handle nil returns defensively where the underlying value has been collected.
- Services MUST ensure that cleanup argument handles (`arg`) contain exclusively minimal primitive identifiers or isolated keys (e.g., string cache keys or uint64 tokens) rather than enclosing struct references.
- Components NEVER trigger synchronized, blocking whole-map scans inside Chi middleware request paths; eviction must remain decoupled and amortization handled via Go 1.24 GC cycle coordination.

## Bad vs Good Implementations
### Migrating from runtime.SetFinalizer to Go 1.24 weak.Make and runtime.AddCleanup in Chi Middleware Cache

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using runtime.SetFinalizer delays object deallocation by at least one full GC cycle, creates object resurrection vulnerabilities, serializes finalization on a single runtime thread, and locks up the cache when high request concurrency creates eviction contention.
```go
package cache

import (
	"net/http"
	"runtime"
	"sync"
)

type CachedResponse struct {
	Payload []byte
}

type MemoryCache struct {
	mu    sync.RWMutex
	items map[string]*CachedResponse
}

func (c *MemoryCache ChiLegacyCache) Put(key string, payload []byte) *CachedResponse {
	c.mu.Lock()
	defer c.mu.Unlock()

	res := &CachedResponse{Payload: payload}
	c.items[key] = res

	// ANTI-PATTERN: runtime.SetFinalizer pins the object, causes resurrection issues,
	// delays collection across multiple GC cycles, and serializes on the runtime finalizer goroutine.
	// Capturing 'res' or enclosing structures leads to severe GC pacing spikes and finalizer starvation.
	runtime.SetFinalizer(res, func(r *CachedResponse) {
		c.mu.Lock()
		delete(c.items, key)
		c.mu.Unlock()
	})

	return res
}

func (c *MemoryCache) Middleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.mu.RLock()
		item, exists := c.items[r.URL.Path]
		c.mu.RUnlock()
		if exists && item != nil {
			w.Write(item.Payload)
			return
		}
		next.ServeHTTP(w, r)
	})
}
```

#### Production Standard (Correct)
> **Standard**: Go 1.24 `weak.Make` allows GC to reclaim `CachedResponse` instantaneously when HTTP responses complete and callers drop references. `runtime.AddCleanup` decouples eviction from target object memory layout, avoiding cycle formation and preventing finalizer queue stalls.
```go
package cache

import (
	"net/http"
	"runtime"
	"sync"
	"weak"
)

type CachedResponse struct {
	Payload []byte
}

type InvariantCache struct {
	mu    sync.RWMutex
	items map[string]weak.Pointer[CachedResponse]
}

func NewInvariantCache() *InvariantCache {
	return &InvariantCache{
		items: make(map[string]weak.Pointer[CachedResponse]),
	}
}

func (c *InvariantCache) Put(key string, payload []byte) *CachedResponse {
	res := &CachedResponse{Payload: payload}
	wk := weak.Make(res)

	c.mu.Lock()
	c.items[key] = wk
	c.mu.Unlock()

	// PROD INVARIANT: runtime.AddCleanup attaches to 'res' without capturing 'res'.
	// Only passes the isolated primitive key to avoid resurrection and memory cycles.
	runtime.AddCleanup(res, func(cleanupKey string) {
		c.mu.Lock()
		// Ensure stale weak pointer is pruned only if it was not replaced
		if existing, found := c.items[cleanupKey]; !found || existing.Value() == nil {
			delete(c.items, cleanupKey)
		}
		c.mu.Unlock()
	}, key)

	return res
}

func (c *InvariantCache) Middleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.mu.RLock()
		wk, exists := c.items[r.URL.Path]
		c.mu.RUnlock()

		if exists {
			if item := wk.Value(); item != nil {
				w.Header().Set("X-Cache", "HIT")
				w.Write(item.Payload)
				return
			}
		}
		next.ServeHTTP(w, r)
	})
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction

Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction defines an industrial-grade behavioral standard for backend engineering. Governs the use of Go 1.24 weak pointers (weak.Pointer) and runtime.AddCleanup within high-throughput Go Chi in-memory cache topologies. Replaces legacy runtime.SetFinalizer patterns and explicit stop-the-world eviction sweeps to eliminate GC pacing spikes, finalizer thread starvation, and cyclic reference memory leaks under extreme HTTP request concurrency. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Legacy runtime.SetFinalizer and Strong Reference Maps Inducing GC Latency Spikes and Memory Leaks Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftLegacy runtime.SetFinalizer and Strong Reference Maps Inducing GC Latency Spikes and Memory LeaksCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS use Go 1.24 `weak.Pointer[T]` via `weak.Make(ptr)` when storing cached payload references in internal cache metadata nodes.

Engineers MUST ALWAYS use Go 1.24 `weak.Pointer[T]` via `weak.Make(ptr)` when storing cached payload references in internal cache metadata nodes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS pair weak pointers with `runtime.AddCleanup(ptr, cleanupFunc, arg)` to schedule side-effect cleanups (such as index removals or off-heap releases) without delaying object sweep phases.

Engineers MUST ALWAYS pair weak pointers with `runtime.AddCleanup(ptr, cleanupFunc, arg)` to schedule side-effect cleanups (such as index removals or off-heap releases) without delaying object sweep phases.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT pass closures to `runtime.AddCleanup` that capture the target object `ptr` either directly or transitively, as this immediately creates a strong cycle and prevents garbage collection.

Engineers MUST NOT pass closures to `runtime.AddCleanup` that capture the target object `ptr` either directly or transitively, as this immediately creates a strong cycle and prevents garbage collection.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST NEVER use `runtime.SetFinalizer`, which pins entire object subgraphs across multiple GC cycles, risks resurrection, and serializes destruction on a single finalizer goroutine.

Engineers MUST NEVER use `runtime.SetFinalizer`, which pins entire object subgraphs across multiple GC cycles, risks resurrection, and serializes destruction on a single finalizer goroutine.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Code MUST explicitly validate pointer dereference using `.Value()` on `weak.Pointer[T]` and handle nil returns defensively where the underlying value has been collected.

Code MUST explicitly validate pointer dereference using `.Value()` on `weak.Pointer[T]` and handle nil returns defensively where the underlying value has been collected.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Services MUST ensure that cleanup argument handles (`arg`) contain exclusively minimal primitive identifiers or isolated keys (e.g., string cache keys or uint64 tokens) rather than enclosing struct references.

Services MUST ensure that cleanup argument handles (`arg`) contain exclusively minimal primitive identifiers or isolated keys (e.g., string cache keys or uint64 tokens) rather than enclosing struct references.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
7

Components NEVER trigger synchronized, blocking whole-map scans inside Chi middleware request paths

Components NEVER trigger synchronized, blocking whole-map scans inside Chi middleware request paths; eviction must remain decoupled and amortization handled via Go 1.24 GC cycle coordination.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Go 1.24 Weak Pointers and Cleanup-Driven Deterministic In-Memory Cache Eviction critical for AI coding assistants?

Governs the use of Go 1.24 weak pointers (weak.Pointer) and runtime.AddCleanup within high-throughput Go Chi in-memory cache topologies. Replaces legacy runtime.SetFinalizer patterns and explicit stop-the-world eviction sweeps to eliminate GC pacing spikes, finalizer thread starvation, and cyclic reference memory leaks under extreme HTTP request concurrency. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Legacy runtime.SetFinalizer and Strong Reference Maps Inducing GC Latency Spikes and Memory Leaks) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Go