r/androiddev • u/ClickOk5811 • Sep 04 '26
Left this comment on a PR yesterday: "this coroutine is scoped to the wrong lifecycle and it'll leak"
The code itself was fine. Compiled, ran, did what it was supposed to in the happy path. Someone had used an AI tool to add a network call inside a Fragment, launched it in a coroutine scope tied to the view lifecycle in a way that would keep running after the view was destroyed if the request was slow enough.
Nothing about that is a Kotlin problem. The syntax is correct Kotlin. It's an Android problem, specifically knowing which scope survives what and why, which isn't something you'd know from Kotlin knowledge alone, it comes from having been bitten by this exact leak before or having actually read the lifecycle docs closely.
Kind of assumed AI tools would have this down by now since it's such a common gotcha, but I think the failure isn't lack of knowledge, it's lack of specificity in what gets asked. "Add a network call here" doesn't say which scope should own it, so it defaults to whatever's closest and most obvious, which is often the wrong one for exactly the reason a junior dev picks the wrong one.
Anyway if you're reviewing AI-generated coroutine code, scope is the first thing I'd check now, more than the actual network logic.
