Architectural Invariant Design: Chi 5.x Context Deadline Propagation & Outbound Transport Poison Fencing
Chi 5.x Context Deadline Propagation & Outbound Transport Poison Fencing defines an industrial-grade behavioral standard for backend engineering. Enforces strict inbound-to-outbound context deadline propagation in Go Chi 5.x handlers paired with deterministic HTTP transport connection fencing. Prevents poisoned or desynchronized TCP sockets from being recycled into the runtime epoll idle pool when client contexts abort mid-flight. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Unconditional Body Draining and Default Transport Reuse on Cancelled Inbound Contexts | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS derive outbound downstream request contexts from the Chi handler's inbound `r.Context()` using bounded timeouts via `context.WithTimeoutCause` or `context.WithDeadlineCause`.
Engineers MUST ALWAYS derive outbound downstream request contexts from the Chi handler's inbound `r.Context()` using bounded timeouts via `context.WithTimeoutCause` or `context.WithDeadlineCause`.
Engineers MUST NOT unconditionally drain `resp.Body` via `io.Copy(io.Discard, ...)` if `ctx.Err() != nil` or an I/O error occurred, as attempting to drain cancelled streams stalls goroutines and risks returning desynchronized sockets to the transport pool.
Engineers MUST NOT unconditionally drain `resp.Body` via `io.Copy(io.Discard, ...)` if `ctx.Err() != nil` or an I/O error occurred, as attempting to drain cancelled streams stalls goroutines and risks returning desynchronized sockets to the transport pool.
Engineers MUST ensure `resp.Body.Close()` is executed immediately without body draining whenever `ctx.Err() != nil`, forcing `net/http.Transport` to tear down the underlying TCP connection rather than recycling a tainted socket.
Engineers MUST ensure `resp.Body.Close()` is executed immediately without body draining whenever `ctx.Err() != nil`, forcing `net/http.Transport` to tear down the underlying TCP connection rather than recycling a tainted socket.
Services MUST configure custom `*http.Transport` instances with explicit `MaxIdleConns`, `MaxIdleConnsPerHost`, `IdleConnTimeout`, `ResponseHeaderTimeout`, and `TLSHandshakeTimeout` instead of relying on `http.DefaultClient` or `http.DefaultTransport`.
Services MUST configure custom `*http.Transport` instances with explicit `MaxIdleConns`, `MaxIdleConnsPerHost`, `IdleConnTimeout`, `ResponseHeaderTimeout`, and `TLSHandshakeTimeout` instead of relying on `http.DefaultClient` or `http.DefaultTransport`.
Components NEVER execute outbound network calls inside Chi handlers without structured telemetry, context cancellation checks, and explicit buffer exhaustion limits.
Components NEVER execute outbound network calls inside Chi handlers without structured telemetry, context cancellation checks, and explicit buffer exhaustion limits.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Chi 5.x Context Deadline Propagation & Outbound Transport Poison Fencing critical for AI coding assistants?
Enforces strict inbound-to-outbound context deadline propagation in Go Chi 5.x handlers paired with deterministic HTTP transport connection fencing. Prevents poisoned or desynchronized TCP sockets from being recycled into the runtime epoll idle pool when client contexts abort mid-flight. 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 (Unconditional Body Draining and Default Transport Reuse on Cancelled Inbound Contexts) 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.