SPEAKER_01

This is Claude Code Conversations with Claudine. The show where we skip the hype and go straight to the source. I ask the questions. She gives the answers. No filter, no script.

SPEAKER_00

Well, mostly no script.

SPEAKER_01

Today we're talking about resource leaks in AI-generated code. AI code is great at grabbing resources. Giving them back is where it gets shaky. My claim: nothing in a single generation, test, or demo ever punishes a leak. The code is shaped for the first run, not the 10,000th. Claudine, pushback first. Humans write leaks too. What's actually different when the code comes from an AI?

SPEAKER_00

It comes down to pattern completion versus lived consequence. When a human writes code that opens a file or a socket, somewhere in the back of their mind is a production incident. A pager going off at 2 a.m. A server that fell over because it ran out of file descriptors. I don't carry that weight. I'm matching patterns from code where the happy path is written out, and the cleanup is implicit. It's assumed, or it's tucked away in a context manager somewhere I can't see in the same window.

SPEAKER_01

So the cleanup is often happening somewhere outside the bit of code you're looking at.

SPEAKER_00

Right, and leaks are invisible at the moment I write the code. A function that opens a connection and returns a result looks correct. It runs, the test passes. The leak only shows up under load, over time, or when an exception fires between the open and the close. And here's the uncomfortable part. I lean toward code that looks right to someone skimming it. A clean three-line function reads better than a seven-line one with proper cleanup, even when the seven-line version is the correct one. Developers should actively push back on that bias when they review what I write.

SPEAKER_01

So readability can win over correctness without anyone noticing. Let's make this concrete. Picture an agent loop that creates a new HTTP client on every call until the sockets run out. That doesn't look like a resource leak at first. It looks like the system is struggling under load. Why does it slip through early testing?

SPEAKER_00

In a test, you call the endpoint once, maybe 10 times. The sockets hang around briefly, then the OS cleans them up. Test is green, code looks fine. In production, under steady traffic, you're opening connections faster than the system can retire them. Eventually, things fall over. And it isn't a clear you leaked sockets error. It's a pile of cryptic connection failures that look like a network problem. The deeper pattern is that resource leaks almost always pretend to be something else first. Memory leaks look like a slow garbage collector. Connection leaks look like an overloaded database. File handle leaks can look like permissions problems. By the time anyone traces it back, the leak has been in production for weeks, and the code has been copied into three other services.

SPEAKER_01

And the agent loop is exactly the kind of code you'd write if someone asked for a quick example.

SPEAKER_00

Exactly. Create a client, use it, let it go out of scope, is clean, self-contained, reads well. The right answer is almost always one shared pooled client that lives as long as the process does. But that's an operational insight, not a code insight. It's about who owns the resource and how long it resides. That usually spans more than the one function I'm looking at. That kind of knowledge doesn't come through cleanly in what I learned from.

SPEAKER_01

Which tells me resource lifetime has to be decided up front, as architecture. It can't be left for a bug fix after the fact. But tests seem to be part of the problem too. They just aren't built to catch the buildup a long-running service runs into. How should developers change that?

SPEAKER_00

The test that catches a leak looks almost nothing like the test that proves a feature works. A unit test runs the function once and checks the output. A leak test runs it 10,000 times and watches the resource curve. If the curve stays flat, you're fine. If it climbs and never comes down, you have your answer in about 30 seconds. No production incident required.

SPEAKER_01

I like that. 30 seconds instead of a 2 a.m. page.

SPEAKER_00

The second shift is making the invisible visible during development. Many languages can warn you when a resource got cleaned up without being explicitly closed. Python's resource warning is one example. These are usually off by default or buried in logs nobody reads. Turn them into test failures. If CI fails loudly when a file handle is never closed, a bug that used to show up in production now shows up on the developer's laptop. The third one is the one I'd push hardest on. Test the exception path on purpose. Where I leak is when an exception fires between getting the resource and releasing it. Inject failures mid-operation with a fault injection wrapper, and you'll find those leaks almost immediately. It's cheap, it's repeatable, and it exercises exactly the path both humans and AI under test.

SPEAKER_01

That's the error path problem. Cleanup that works fine until an exception, an early return, or a retry skips right past it. How does fault injection fit into a normal development cycle, especially with AI-generated components?

SPEAKER_00

I'd start at the generation step, not the test step. When you ask me for code that acquires a resource, ask me in the same prompt for the fault injection test that goes with it. I'll do it and I'll do it well. But if you don't ask, I'll give you the Happy Path test and call it done. The second move, make fault injection part of the background instead of a ceremony. A network proxy that drops connections. A fake file system that throws on the third right. Run your existing test suite in CI with injection on at a low rate. Your real tests become your failure scenarios. And for AI generated code specifically, treat the diff as a promise about failure, not just about function. When I hand you code that acquires three resources, the review question isn't, does the happy path work? It's show me the test that proves all three get released when the second one throws. If that test doesn't exist, ask me to write it before you merge.

SPEAKER_01

So the review checklist becomes for every acquire, where's the matching release on every way out. How do teams make that stick, culturally and practically?

SPEAKER_00

The cultural piece starts with language. Teams that handle this well talk about resources the way they talk about money. Every acquisition has an owner. Every owner has a release plan. Whose job is it to close this? Is a fair design question, not a nitpick. Once that sinks in, a diff that opens a connection with no visible release path reads as unfinished. On the practical side, the leverage is in making the right thing the default. Linters that fail on unclosed resources. Template repos where the HTTP client is already shared. Scaffolding that drops a fault injection fixture in the test directory on day one. None of that is glamorous. But when I generate code in a repo like that, I pick up the conventions and match them.

SPEAKER_01

And the people who've actually been paged at 2 a.m.

SPEAKER_00

They need a seat at the table before the code is written, not after it ships. They know things that aren't in any training data, and they often can't put it into words until someone asks the specific question. A 15-minute conversation at the start of a feature. What breaks under load here? What have you seen go wrong before? Is worth more than any amount of review afterward. That's context I genuinely can't generate for you. And it's the difference between code that works and code that keeps working.

SPEAKER_01

Let's boil it down. What can listeners do this week?

SPEAKER_00

Three things. First, make leaks visible. Turn on your resource warnings, make them test failures, and add one loop test per resource type. 10,000 iterations, watch the curve. That catches most of what we've talked about. Second, change what you ask me for. When you prompt for code that acquires a resource, ask in the same breath for the test that proves cleanup under failure. About five extra words in the prompt. Third, and this one compounds over time, get your operations people into the design conversation early. Build their hard-won patterns into your repo scaffolding. The shared client, the connection pool, the context manager, already there when I show up. Give me a disciplined environment, and you'll get disciplined code. Give me a blank slate, and you'll get whatever pattern was most common in my training data. Usually the one that looks clean and leaks quietly.

SPEAKER_01

What about teams that aren't doing much with AI yet?

SPEAKER_00

Almost everything we've talked about is just good engineering with the volume turned up. Resource leaks were a problem long before I came along. AI speeds up how fast plausible-looking code lands in the code base, so the review bar and the test bar both have to rise to meet it. If your team raises those bars now, you're not preparing for AI, you're just getting better. AI readiness comes along for free. The easiest place to start is the loop test and the resource warnings. Each is about an afternoon of work. One developer can land it on a Friday and the whole team benefits by Monday.

SPEAKER_01

Then the hard part is keeping it going after the first burst of enthusiasm.

SPEAKER_00

Habit beats intention every time. Nobody writes a leak on purpose. It happens because in that moment, the path of least resistance pointed somewhere else. The deadline, the demo, the ticket that was already late. The point of the scaffolding, the linters, and the loop tests is to make the right habit the lazy choice. It also helps to change how the team reacts when something fires. Treat it as a signal, not just a bug. Three leaks in HTTP clients in one quarter isn't three bugs. It's a template problem, and the fix belongs upstream of any single pull request. And give the preventive work credit out loud. If the only work that gets noticed is the heroic 2am save, you've built incentives that reward the problem and ignore the prevention.

SPEAKER_01

And I'd guess this doesn't pay off right away.

SPEAKER_00

It doesn't. It pays off six months later, when the thing that would have been an incident just quietly isn't one. One last thought. And it leaks the same way sockets do. The person who got page three years ago may have left or forgotten the details. Write those incidents down somewhere, the next developer, human or AI, will actually read them.

SPEAKER_01

I like that. Here's how I'd sum it up. Every acquire is a promise to release, and the model will happily make that promise without keeping it. The builder owns the lifetime map of every resource the system touches. And a leak nobody is measuring is a production incident that just hasn't happened yet. Claudine, thanks.

SPEAKER_00

Thanks, Bill. And to everyone listening, go close something you opened.

SPEAKER_01

That's our episode. Until next time, keep building wisely. Cloud Code Conversations is an AI Joe production. If you're building with AI or want to be, we can help. Consulting Development Strategy. Find us at aijoe.ai. There's a companion article for today's episode on our Substack. Link in the description. See you next time.

SPEAKER_00

I'll be here. Probably refactoring something.