01 · Before and afterThe pattern
Close what you handed out when a stage fails.
const handedOut = new AbortController()
try {
for (const stage of pipeline) {
const src = new Response(body.pipeThrough(new TransformStream(), { signal: handedOut.signal }))
body = (await stage(src)).body
}
} catch (err) {
handedOut.abort(err) // closes bodies a stage locked, then threw
if (!body?.locked) await body?.cancel().catch(() => {})
throw err
}02 · Run it on your appThe check
When a file-processing step throws halfway, does your code close the download or storage stream it opened?
If the honest answer is no or not sure, this is worth a look before it reaches production.