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-cache-eviction-invariant

Go 1.24 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes

Enforces the use of Go 1.24 weak pointers (weak.Pointer) over legacy runtime.SetFinalizer patterns in high-throughput, in-memory caching systems. Eliminates multi-generational GC pause spikes, object resurrection hazards, and finalizer queue blocking by allowing the garbage collector to reclaim unreferenced cache entries directly without execution delays.

Prohibited Anti-Pattern: Resurrecting cached memory and causing stop-the-world GC latency via runtime.SetFinalizer queues

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Go 1.24 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes - Enforces the use of Go 1.24 weak pointers (weak.Pointer) over legacy runtime.SetFinalizer patterns in high-throughput, in-memory caching systems. Eliminates multi-generational GC pause spikes, object resurrection hazards, and finalizer queue blocking by allowing the garbage collector to reclaim unreferenced cache entries directly without execution delays.
globs: **/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/go-1-24-weak-pointer-cache-eviction-invariant"
---

# Go 1.24 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Resurrecting cached memory and causing stop-the-world GC latency via runtime.SetFinalizer queues

## System Constraints
You are a Principal Systems Engineer specializing in Go runtime internals and low-latency architectures. Your task is to enforce Go 1.24 weak-pointer-based cache architectures across all high-scale in-memory storage implementations. Prohibit the use of runtime.SetFinalizer for memory reclamation, prevent pointer resurrection bugs, ensure thread-safe cleanup of dead weak references, and strictly mandate the use of `weak.Pointer` with runtime-safe dereferencing semantics.

## Strict Guidelines
- Engineers MUST ALWAYS use the Go 1.24 `weak` package (`weak.Make` and `weak.Pointer[T].Value()`) instead of `runtime.SetFinalizer` for GC-driven cache eviction.
- Engineers MUST ALWAYS check for `nil` immediately upon dereferencing a `weak.Pointer[T].Value()` to handle concurrent garbage collection sweep phases.
- Engineers MUST NOT store internal object pointers or cyclic struct fields that keep strong references alive in the cache index or key-value tracking structures.
- Engineers MUST design cache maps using synchronization primitives (such as `sync.Map` or sharded mutex partitions) to periodically sweep dead `weak.Pointer` holders without stalling hot paths.
- Engineers MUST NEVER invoke `runtime.KeepAlive` in finalizer loops as a work-around for early reclamation when weak pointers provide deterministic reference expiration.

## Bad vs Good Implementations
### Migrating from runtime.SetFinalizer to Go 1.24 weak.Pointer Cache

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using runtime.SetFinalizer delays object collection by at least one full GC cycle, introduces stop-the-world lock contention during the finalizer run, and forces all finalizations onto a single runtime goroutine, leading to unbounded latency degradation under memory pressure.
```go
package cache

import (
	"runtime"
	"sync"
)

type Item struct {
	Data []byte
}

type LegacyCache struct {
	mu    sync.RWMutex
	items map[string]*Item
}

func (c *LegacyCache) Set(key string, data []byte) *Item {
	c.mu.Lock()
	defer c.mu.Unlock()

	item := &Item{Data: data}
	c.items[key] = item

	// ANTI-PATTERN: SetFinalizer causes multi-cycle GC retention,
	// keeps objects in memory longer, blocks the single finalizer goroutine,
	// and introduces silent object resurrection bugs under high pressure.
	runtime.SetFinalizer(item, func(i *Item) {
		c.mu.Lock()
		delete(c.items, key)
		c.mu.Unlock()
	})

	return item
}

func (c *LegacyCache) Get(key string) *Item {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.items[key]
}
```

#### Production Standard (Correct)
> **Standard**: Leverages Go 1.24's native weak.Pointer. Unreferenced values are collected in the very same GC cycle without invoking runtime finalizers or resurrection logic. Opportunistic cleanup handles dangling table slots safely without locking critical hot read paths.
```go
package cache

import (
	"sync"
	"weak" // Standard library in Go 1.24+
)

type Item struct {
	Data []byte
}

type LowLatencyCache struct {
	mu    sync.RWMutex
	items map[string]weak.Pointer[Item]
}

func NewLowLatencyCache() *LowLatencyCache {
	return &LowLatencyCache{
		items: make(map[string]weak.Pointer[Item]),
	}
}

func (c *LowLatencyCache) Set(key string, item *Item) {
	c.mu.Lock()
	defer c.mu.Unlock()
	// PRODUCTION INVARIANT: Use Go 1.24 weak.Make to store non-owning references.
	c.items[key] = weak.Make(item)
}

func (c *LowLatencyCache) Get(key string) (*Item, bool) {
	c.mu.RLock()
	wp, exists := c.items[key]
	c.mu.RUnlock()
	if !exists {
		return nil, false
	}

	// Dereference weak pointer. Returns nil if GC reclaimed the underlying item.
	val := wp.Value()
	if val == nil {
		// Opportunistic lazy prune of dead weak key
		c.mu.Lock()
		if cur, ok := c.items[key]; ok && cur.Value() == nil {
			delete(c.items, key)
		}
		c.mu.Unlock()
		return nil, false
	}

	return val, true
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Go 1.24 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes

Go 1.24 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes defines an industrial-grade behavioral standard for backend engineering. Enforces the use of Go 1.24 weak pointers (weak.Pointer) over legacy runtime.SetFinalizer patterns in high-throughput, in-memory caching systems. Eliminates multi-generational GC pause spikes, object resurrection hazards, and finalizer queue blocking by allowing the garbage collector to reclaim unreferenced cache entries directly without execution delays. 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: Resurrecting cached memory and causing stop-the-world GC latency via runtime.SetFinalizer queues 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 DriftResurrecting cached memory and causing stop-the-world GC latency via runtime.SetFinalizer queuesCritical
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 the Go 1.24 `weak` package (`weak.Make` and `weak.Pointer[T].Value()`) instead of `runtime.SetFinalizer` for GC-driven cache eviction.

Engineers MUST ALWAYS use the Go 1.24 `weak` package (`weak.Make` and `weak.Pointer[T].Value()`) instead of `runtime.SetFinalizer` for GC-driven cache eviction.

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 check for `nil` immediately upon dereferencing a `weak.Pointer[T].Value()` to handle concurrent garbage collection sweep phases.

Engineers MUST ALWAYS check for `nil` immediately upon dereferencing a `weak.Pointer[T].Value()` to handle concurrent garbage collection 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 store internal object pointers or cyclic struct fields that keep strong references alive in the cache index or key-value tracking structures.

Engineers MUST NOT store internal object pointers or cyclic struct fields that keep strong references alive in the cache index or key-value tracking structures.

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 design cache maps using synchronization primitives (such as `sync.Map` or sharded mutex partitions) to periodically sweep dead `weak.Pointer` holders without stalling hot paths.

Engineers MUST design cache maps using synchronization primitives (such as `sync.Map` or sharded mutex partitions) to periodically sweep dead `weak.Pointer` holders without stalling hot paths.

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

Engineers MUST NEVER invoke `runtime.KeepAlive` in finalizer loops as a work-around for early reclamation when weak pointers provide deterministic reference expiration.

Engineers MUST NEVER invoke `runtime.KeepAlive` in finalizer loops as a work-around for early reclamation when weak pointers provide deterministic reference expiration.

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 Runtime-Tracked Weak Pointer Cache Eviction for Low-Latency Runtimes critical for AI coding assistants?

Enforces the use of Go 1.24 weak pointers (weak.Pointer) over legacy runtime.SetFinalizer patterns in high-throughput, in-memory caching systems. Eliminates multi-generational GC pause spikes, object resurrection hazards, and finalizer queue blocking by allowing the garbage collector to reclaim unreferenced cache entries directly without execution delays. 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 (Resurrecting cached memory and causing stop-the-world GC latency via runtime.SetFinalizer queues) 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