Leaked resources: files, connections, goroutines
Spot files, connections and locks opened without `with`, try-with-resources or `defer`, as well as `defer` inside loops, goroutines blocked forever and contexts never cancelled. Explain why these pass tests but exhaust resources in production.
Builds on: Error handling idioms across languages
Green tests, then 'too many open files'
You asked the agent to add an export endpoint that reads rows from the database and writes them to a file. The diff is tidy, tests are green, and it ships. Three days later the service fails with `too many open files`, and requests hang waiting for a database connection. A restart fixes it, for about a day. Nothing in the diff looked wrong.
Why leaks pass every test
Tests are short. They make a few calls, then the process exits and the OS reclaims every file and socket, so a leak keeps the proxy signal green. The agent writes the shortest code that works on the path it tried, and a `close()` on the success line looks finished. Leaks hide on error paths and early returns, and they only add up after thousands of calls. **Later cost:** exhausted file descriptors, a drained connection pool, locks held forever, and goroutine memory that keeps climbing.
Tells: a release not tied to scope
- **Python:** `open`, `connect` or `acquire()` outside a `with` block, or a manual `close()` placed after other calls.
- **Java:** connections, statements or streams not declared in try-with-resources; `unlock()` not inside a `finally`.
- **Go:** no `defer x.Close()` right after the error check; `defer` inside a loop; `resp.Body` never closed; `cancel` assigned to `_`; a goroutine that sends on a channel nobody will read.
For each one, ask: what releases this on the error path?
A lock that is never released
In Python, the agent adds a cache refill: `lock.acquire()`, then `rows = db.fetch()`, then `lock.release()`. Tests pass. In production, `db.fetch()` times out once. The exception skips `release()`, the lock stays held, and every later request blocks on `acquire()` forever. No garbage collector frees a held lock. **Tell:** acquire and release written as separate statements. **Later cost:** one timeout freezes the feature until a restart. **BETTER:** wrap the fetch in `with lock:`, which releases the lock on return and on exception alike.
Go: goroutines blocked forever
BAD: the agent's helper returns the first URL that answers, and it returns the right one in tests. **Tell:** an unbuffered channel with many senders but only one receive. Every goroutine except the first to succeed blocks forever on `ch <- u`, so its deferred `Body.Close()` never runs and it keeps a connection. If every request fails, nothing is sent and the caller blocks forever. **Later cost:** goroutines and connections pile up on every call. **BETTER:** use `make(chan string, len(urls))` so every send completes. Have each goroutine send on failure too, for example `""`, and let the caller receive up to `len(urls)` times. Build the requests with a context whose `cancel` the caller defers, which stops the losing requests.
// Go
func firstOK(urls []string) string {
ch := make(chan string)
for _, u := range urls {
go func(u string) {
r, err := http.Get(u)
if err != nil { return }
defer r.Body.Close()
ch <- u
}(u)
}
return <-ch
}
Every acquire needs a scoped release
Tie every release to scope: a `with` block, try-with-resources, `unlock()` in `finally`, or `defer` right after the error check. Never put `defer` directly in a loop body; move the body into its own function instead. Every goroutine needs a guaranteed way to finish, and every `cancel` must be called. Tests won't show the gap, so ask what frees this on the error path and after 10,000 calls.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.