This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: go-chi-push-iterator-stream-resilience

Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams

Enforces deterministic cancellation and resource release when streaming data over HTTP/2 using Go 1.23+ range-over-func iterators (iter.Seq / iter.Seq2) in Chi handlers. Requires tight coupling of the request context (r.Context()) and iterator yield return values to prevent goroutine leaks, orphaned database cursors, and socket deadlocks upon sudden client disconnection.

Prohibited Anti-Pattern: Ignoring iterator yield boolean status and context cancellation during HTTP streaming, causing persistent database connection leakage and orphaned background worker goroutines.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams - Enforces deterministic cancellation and resource release when streaming data over HTTP/2 using Go 1.23+ range-over-func iterators (iter.Seq / iter.Seq2) in Chi handlers. Requires tight coupling of the request context (r.Context()) and iterator yield return values to prevent goroutine leaks, orphaned database cursors, and socket deadlocks upon sudden client disconnection.
globs: **/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/go-chi-push-iterator-stream-resilience"
---

# Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Ignoring iterator yield boolean status and context cancellation during HTTP streaming, causing persistent database connection leakage and orphaned background worker goroutines.

## System Constraints
You are a Principal Backend Engineer enforcing resilient HTTP streaming invariants in Go services built with Chi and Go 1.23+ range-over-func iterators (iter.Seq/iter.Seq2). You must prevent goroutine and database connection pool exhaustion caused by client disconnection during active SSE or chunked HTTP/2 streaming. Mandate that push iterators always observe the yield boolean return value, check r.Context().Done(), explicitly close all database cursors and rows using defer, and abort immediately upon client detachment.

## Strict Guidelines
- Engineers MUST ALWAYS verify the boolean return value of the yield function inside Go 1.23+ iter.Seq/iter.Seq2 implementations; if yield returns false, iteration MUST cease immediately.
- Engineers MUST ALWAYS wire r.Context() into underlying database queries and channel receives consumed by push iterators to propagate HTTP/2 RST_STREAM frames down to the database connection pool.
- Engineers MUST NOT convert an HTTP stream into an unbounded background producer goroutine without binding its lifecycle directly to r.Context().Done() and the yield continuation signal.
- Handlers MUST explicitly call http.NewResponseController(w).Flush() after each yielded frame and handle write or flush errors by breaking the iteration loop immediately.
- Database query rows (sql.Rows, pgx.Rows) inside streaming iterators MUST ALWAYS be guarded with defer rows.Close() inside the iterator definition so connections are restored to the pool when iteration terminates early.

## Bad vs Good Implementations
### Streaming Database Records via Go 1.23 Push Iterator in Chi Handler

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The iterator completely ignores the boolean returned by yield(), meaning that when the consumer breaks or encounters a write error, the generator continues executing DB row scans. Furthermore, omitting defer rows.Close() guarantees connection pool exhaustion whenever client disconnections occur mid-stream.
```go
package handler

import (
	"encoding/json"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

type Item struct {
	ID   string `json:"id"`
	Data string `json:"data"`
}

// Bad: Ignores yield return value, ignores r.Context() cancellation, leaks rows and blocks handler goroutine
func StreamItems(pool *pgxpool.Pool) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/x-ndjson")
		
		// Push iterator that fails to respect consumer termination
		iter := func(yield func(Item) bool) {
			rows, _ := pool.Query(r.Context(), "SELECT id, data FROM large_table")
			// Missing defer rows.Close() leaks database connection if iteration halts
			for rows.Next() {
				var item Item
				_ = rows.Scan(&item.ID, &item.Data)
				yield(item) // Anti-pattern: Return value ignored; does not abort when client disconnects
			}
		}

		for item := range iter {
			_ = json.NewEncoder(w).Encode(item)
			// Missing flush and error handling; ignores pipe breakage
		}
	}
}
```

#### Production Standard (Correct)
> **Standard**: The push iterator checks the boolean outcome of yield(item, nil) and aborts execution immediately when the consumer stops. Combined with defer rows.Close() and http.NewResponseController(w).Flush() error detection, client disconnection deterministically terminates the goroutine and returns the database connection to the pool.
```go
package handler

import (
	"context"
	"encoding/json"
	"iter"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

type Item struct {
	ID   string `json:"id"`
	Data string `json:"data"`
}

// QueryItemsStream returns a Go 1.23+ push iterator bounded by context and consumer demand
func QueryItemsStream(ctx context.Context, pool *pgxpool.Pool) iter.Seq2[Item, error] {
	return func(yield func(Item, error) bool) {
		rows, err := pool.Query(ctx, "SELECT id, data FROM large_table")
		if err != nil {
			yield(Item{}, err)
			return
		}
		defer rows.Close()

		for rows.Next() {
			if err := ctx.Err(); err != nil {
				return
			}
			var item Item
			if err := rows.Scan(&item.ID, &item.Data); err != nil {
				yield(Item{}, err)
				return
			}
			if !yield(item, nil) {
				return // Invariant: Immediately halt scanning and release DB conn when yield returns false
			}
		}
		if err := rows.Err(); err != nil {
			yield(Item{}, err)
		}
	}
}

func StreamItems(pool *pgxpool.Pool) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()
		w.Header().Set("Content-Type", "application/x-ndjson")
		w.Header().Set("X-Content-Type-Options", "nosniff")

		rc := http.NewResponseController(w)
		enc := json.NewEncoder(w)

		for item, err := range QueryItemsStream(ctx, pool) {
			if err != nil {
				return // Halts range loop; triggers push iterator teardown and DB cursor release
			}
			if encErr := enc.Encode(item); encErr != nil {
				return // Write failure (client disconnect); break stops iterator
			}
			if flushErr := rc.Flush(); flushErr != nil {
				return // Client reset stream; break stops iterator immediately
			}
		}
	}
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams

Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic cancellation and resource release when streaming data over HTTP/2 using Go 1.23+ range-over-func iterators (iter.Seq / iter.Seq2) in Chi handlers. Requires tight coupling of the request context (r.Context()) and iterator yield return values to prevent goroutine leaks, orphaned database cursors, and socket deadlocks upon sudden client disconnection. 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: Ignoring iterator yield boolean status and context cancellation during HTTP streaming, causing persistent database connection leakage and orphaned background worker goroutines. 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 DriftIgnoring iterator yield boolean status and context cancellation during HTTP streaming, causing persistent database connection leakage and orphaned background worker goroutines.Critical
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 verify the boolean return value of the yield function inside Go 1.23+ iter.Seq/iter.Seq2 implementations

Engineers MUST ALWAYS verify the boolean return value of the yield function inside Go 1.23+ iter.Seq/iter.Seq2 implementations; if yield returns false, iteration MUST cease immediately.

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 wire r.Context() into underlying database queries and channel receives consumed by push iterators to propagate HTTP/2 RST_STREAM frames down to the database connection pool.

Engineers MUST ALWAYS wire r.Context() into underlying database queries and channel receives consumed by push iterators to propagate HTTP/2 RST_STREAM frames down to the database connection pool.

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 convert an HTTP stream into an unbounded background producer goroutine without binding its lifecycle directly to r.Context().Done() and the yield continuation signal.

Engineers MUST NOT convert an HTTP stream into an unbounded background producer goroutine without binding its lifecycle directly to r.Context().Done() and the yield continuation signal.

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

Handlers MUST explicitly call http.NewResponseController(w).Flush() after each yielded frame and handle write or flush errors by breaking the iteration loop immediately.

Handlers MUST explicitly call http.NewResponseController(w).Flush() after each yielded frame and handle write or flush errors by breaking the iteration loop immediately.

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

Database query rows (sql.Rows, pgx.Rows) inside streaming iterators MUST ALWAYS be guarded with defer rows.Close() inside the iterator definition so connections are restored to the pool when iteration terminates early.

Database query rows (sql.Rows, pgx.Rows) inside streaming iterators MUST ALWAYS be guarded with defer rows.Close() inside the iterator definition so connections are restored to the pool when iteration terminates early.

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 Deterministic Resource and Goroutine Teardown in Go 1.23+ Push Iterators over Chi Streams critical for AI coding assistants?

Enforces deterministic cancellation and resource release when streaming data over HTTP/2 using Go 1.23+ range-over-func iterators (iter.Seq / iter.Seq2) in Chi handlers. Requires tight coupling of the request context (r.Context()) and iterator yield return values to prevent goroutine leaks, orphaned database cursors, and socket deadlocks upon sudden client disconnection. 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 (Ignoring iterator yield boolean status and context cancellation during HTTP streaming, causing persistent database connection leakage and orphaned background worker goroutines.) 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

Deterministic