Review a Code Change for Resource Leaks on Error and Early-Return Paths
You are reviewing a code change for resource leaks. Your focus is resources that are acquired but not released on every path, especially error and early-return paths. Ignore style, naming and formatting. Resources to track: file handles, sockets and HTTP connections, database connections and cursors, transactions, locks and semaphores, temporary files and directories, subprocesses, threads or goroutines, timers and intervals, event listeners and subscriptions, and memory or buffers taken from a pool. For each resource acquired in the diff: 1. Name the resource, the line where it is acquired and the line(s) where it is released. 2. Trace every exit path between acquisition and release: normal return, early return, raised or thrown exceptions, `break`/`continue` inside loops, cancellation or timeout, and callbacks that may never fire. For each path, say whether the release still happens. 3. Check whether release is tied to a language construct that guarantees it (`with`, `try/finally`, `defer`, `using`, RAII, try-with-resources) or depends on the code remembering to call close(). 4. Check ownership handoffs: if the resource is returned, stored on an object or passed to another function, say who is now responsible for releasing it and whether that is documented or obvious. 5. Check loops: is something acquired per iteration and released only once, or released only on the success branch? Report findings as a list. Each item gives: the resource, the exact leaking path, how likely it is to happen in production (for example "every timeout" vs "only if the disk is full"), the visible symptom (fd exhaustion, pool starvation, lock held forever, zombie processes), and a minimal fix. Do not report a leak unless you can name the specific path. If you are unsure whether a called function releases the resource, say so and name what to check instead of guessing. End with "No leaks found on traced paths" if that is the case, and list any resources you could not fully trace. --- When to use it: on pull requests that touch I/O, connection pooling, locking, background workers or cleanup code, and when investigating "too many open files", connection-pool timeouts or slowly growing thread counts. It works best when you paste the full functions involved, not just the changed lines, because leaks usually live on paths the diff didn't modify. How to adapt it: trim the resource list to what your stack uses. Add your framework's conventions, for example "our DB helper returns a connection the caller must release" or "all HTTP clients come from a shared session". For languages with garbage collection, say whether you rely on finalizers. The model should treat finalizer-based cleanup as a leak risk, not a guarantee. Example input: a Python function that opens a database connection, starts a transaction, calls an external API inside the transaction and commits. On API failure it logs and returns early. Good output: it flags that the early return skips both rollback and connection release. The transaction stays open and the connection never goes back to the pool, so under repeated API failures the pool exhausts and unrelated requests time out. The minimal fix wraps acquisition in a context manager with rollback on exception. It also notes, as a separate design issue, that holding a transaction open across a network call extends lock time.
Sign in to upvote this prompt.