Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Leaked resources: files, connections, goroutines
card 6 of 6
Every acquire needs a scoped release
builds on: Error handling idioms across languages
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.
Terms on this card
- goroutine
- A lightweight concurrent function in Go, started with `go f()`. It lives until its function returns; a goroutine blocked forever is never freed.
- defer
- A Go statement that schedules a call to run when the surrounding function returns, not when the current loop iteration or block ends.
- try-with-resources
- Java's `try (var x = ...) { }` form. It calls `close()` on each declared resource when the block exits, normally or by exception.
- finally
- A block after `try` that always runs, whether the try block returned normally or threw an exception. It is used for cleanup.
- cancel
- The function returned by Go's `context.WithCancel` or `WithTimeout`. Calling it stops work tied to the context and frees its timer and bookkeeping.
Lesson goal: 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. ·
the whole lesson