Skip to content

Archive

Resource Management

6 articles
Software Engineering 19 Sep 2026 6 min read

Generation Counters Keep Reused Handles Bound to the Right Resource

A compact handle often looks like an integer because an integer is cheap to store, copy, compare, and pass across an API boundary. In a table-backed resource manager, that integer may simply be an index into a slot array. The representation works until a slot is released and later reused. An old handle can then point at a new resource that happens to occupy the same index. The failure is not an out-of-bounds access. The index can be perfectly valid. The problem is identity: the handle names a storage location, while the caller treats it as the identity of the resource that once occupied that location.

Software Engineering 13 Sep 2026 7 min read

Garbage Collection Does Not Close External Resources

An object can become unreachable while a file descriptor, socket, database transaction, or operating-system lock associated with it still has a meaningful external lifetime. Garbage collection can reclaim managed memory after reachability disappears. It does not, by that fact alone, perform the protocol operation that releases an external resource. The distinction is structural. A collector reasons about references inside a managed heap. A resource such as a file descriptor is an entry maintained by the operating system, and a database transaction is state maintained by another component. Their release semantics come from APIs and protocols outside the collector’s reachability model.

Python 07 Sep 2026 11 min read

Manage Dynamic Resource Lifetimes in Python with ExitStack

A normal with statement works best when you know the resources before the block starts: with open("input.csv", "rb") as source, open("output.csv", "wb") as target: ... The structure is clear because both files are known in advance. Python enters each context manager and guarantees that their exit logic runs when the block finishes, including when an exception leaves the block.

Python 07 Sep 2026 11 min read

Create and Clean Up Temporary Files Safely in Python

Temporary files appear in more programs than their name suggests. A command-line tool may need scratch space while transforming a large file. A test may need an isolated directory. A program may need to hand a real filesystem path to another process and remove it afterward. The risky part is not writing the bytes. It is choosing a name, creating the file without a race, deciding who owns cleanup, and handling differences between a file object and a filesystem path.

Go 03 Sep 2026 10 min read

Use defer for Reliable Cleanup in Go

Resource cleanup is easy to get right on the happy path and easy to miss on an early return. A function opens a file, acquires a lock, or starts a trace span; a later check fails; the function returns before reaching the cleanup statement. Go’s defer statement addresses this by scheduling a function call to run when the surrounding function returns. Used well, it places cleanup next to acquisition and makes every return path easier to reason about.

Python 01 Sep 2026 4 min read

Python Context Managers for Reliable Resource Cleanup

Python’s with statement is more than convenient file syntax. It is a general protocol for pairing setup with guaranteed cleanup, including when code exits early or raises an exception. Understanding context managers makes resource lifetimes visible and prevents a broad class of leaked files, locks, connections, and temporary state. Why try/finally is the foundation Without a context manager, safe cleanup often looks like this: file = open("input.txt", encoding="utf-8") try: data = file.read() finally: file.close() The finally block runs whether the read succeeds or raises. A with statement packages that pattern: