agh-cleanup-failure-paths
Installation
SKILL.md
Cleanup Failure Paths
Hermes review issue #001 cost a real PR round because a procCtx leaked when registry registration failed. The happy path was clean; the partial-failure path leaked. This pattern recurs in every AGH PR that adds multi-step setup. Activate this skill before editing any function that creates/extends a context, registers a resource, opens a connection, or spawns a subprocess.
Procedures
Step 1: Identify Setup-Step Boundaries
- Read the target function and enumerate every "step" that allocates a resource: context creation, file open, listener bind, registry register, lease claim, goroutine spawn, subprocess start, HTTP request, mutex lock, transaction begin.
- For each step, identify the cleanup action that pairs with it:
cancel(),Close(),Unregister(),Release(),Stop(),defer cancel(),tx.Rollback(). - Read
references/cleanup-table.mdfor the canonical pairing table.
Step 2: Walk Every Error Return
- List every
return ... errstatement in the function. - For each one, walk backward through the function body and confirm every resource allocated above the return point is cleaned up — either by an immediate
deferadjacent to its allocation OR by an explicit cleanup call before the return. - If a resource is allocated and the only cleanup is on the success path, that's a leak. Add cleanup on the error path.
- Treat panic recovery and
runtime.Goexitas additional exit paths —defercovers both.