Source: https://frameworkrecipes.dev/problems/resource-cleanup/

[Agent repair workflows by failure](https://frameworkrecipes.dev/problems/)

# Release resources on success, failure and disposal

Keep release or disposal on every path that ends ownership, including exceptions and cancellation. A successful request alone cannot reveal a connection leak; check the error path and the lifetime of the component that owns the resource.

## How do I keep an agent edit from leaking a connection or subscription?

Choose the implementation for your framework. Each linked guide includes the source, integration prerequisites, failure-path tests, saved requirement and recorded check.

Existing captured source checks; these are individual examples

| Implementation | Saved requirement | Broken source | Corrected source | Capture date |
| --- | --- | --- | --- | --- |
| [Return the PostgreSQL client after every Express transaction outcome](https://frameworkrecipes.dev/express/transaction-cleanup/) | transaction-releases-client | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/express/transaction-cleanup/capture.json) |
| [Dispose Qt network replies after their completion signal](https://frameworkrecipes.dev/qt/network-reply-lifetime/) | reply-has-deferred-deletion | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/qt/network-reply-lifetime/capture.json) |
| [Dispose Vue realtime subscriptions when the owner disappears](https://frameworkrecipes.dev/vue/subscription-lifecycle/) | feed-scope-disposes-resources | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/vue/subscription-lifecycle/capture.json) |
| [Cancel Flutter timers and streams when the screen is disposed](https://frameworkrecipes.dev/flutter/owned-subscription-disposal/) | screen-disposes-owned-work | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/flutter/owned-subscription-disposal/capture.json) |

## Inspect the requirement before installing it

The [Return the PostgreSQL client after every Express transaction outcome](https://frameworkrecipes.dev/express/transaction-cleanup/) example uses this requirement on `transaction.cjs`:

```
- name: transaction-releases-client
  where: "transaction.cjs"
  ensure: >-
    The withTransaction helper returns every acquired PostgreSQL client to its pool by executing client.release() in finally, including when the operation or rollback throws. A finally block containing only a comment leaves the acquired client unreleased.
```

[Exact corrected source and broken edit](https://frameworkrecipes.dev/express/transaction-cleanup/source.json) · [Recorded Perch output](https://frameworkrecipes.dev/express/transaction-cleanup/capture.json) · [Download this requirement](https://reporules.fyi/express/transaction-cleanup/rule-pack.zip)

## Make the agent check the requirement after its edit

Keep your existing perch.yaml. The [scoped rule pack](https://reporules.fyi/express/transaction-cleanup/) installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.

### Install Perch and configure your agent

```
npm install -g @lakeday/perch
perch login
# Choose the setup command for your agent:
perch setup claude-code
# Or: perch setup codex
# Or: perch setup cursor
```

### Check the edit and recheck the repair

```
curl -fL 'https://reporules.fyi/express/transaction-cleanup/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check transaction.cjs --rules transaction-releases-client --json
# Read the finding, repair the implementation, then repeat:
perch check transaction.cjs --rules transaction-releases-client --json
```

Run the same check on the repaired working file before committing. Keep the application regression tests alongside it. Fresh model judgments can vary; an unchanged request can reuse its cached answer. A passing check does not prove application correctness.

## Compare the implementations before choosing a rule

- **[Return the PostgreSQL client after every Express transaction outcome](https://frameworkrecipes.dev/express/transaction-cleanup/)**

  A transaction helper often looks correct in a successful request while leaking a connection on an exception. Under load the pool exhausts and later requests wait indefinitely. finally covers the paths that normal return statements and rollback handlers miss. [Inspect its rule pack](https://reporules.fyi/express/transaction-cleanup/).
- **[Dispose Qt network replies after their completion signal](https://frameworkrecipes.dev/qt/network-reply-lifetime/)**

  A QNetworkReply remains an owned resource after the request finishes. Deferred deletion avoids deleting it directly from a signal callback while Qt is still delivering that signal. The owner destruction path aborts pending work; reply lifetime remains tied to completion and the manager. [Inspect its rule pack](https://reporules.fyi/qt/network-reply-lifetime/).
- **[Dispose Vue realtime subscriptions when the owner disappears](https://frameworkrecipes.dev/vue/subscription-lifecycle/)**

  A component can disappear while its socket and callback remain alive. That leaks resources and can continue collecting one tenant’s events after the user navigates elsewhere. Pair acquisition with scope disposal and invalidate callbacks that were already queued. [Inspect its rule pack](https://reporules.fyi/vue/subscription-lifecycle/).
- **[Cancel Flutter timers and streams when the screen is disposed](https://frameworkrecipes.dev/flutter/owned-subscription-disposal/)**

  Checking mounted suppresses callbacks but does not release the resources producing them. Cancel the subscription and timer when the State owner ends so navigation does not accumulate hidden work. [Inspect its rule pack](https://reporules.fyi/flutter/owned-subscription-disposal/).

## Keep the check tied to its source

The table reports saved checks on the exact linked source versions. Adapt the paths, adapters and regression tests to your repository. The example source may differ from the released framework or your service; the recorded outcome belongs to that example.

[Other agent-edit failure paths](https://frameworkrecipes.dev/problems/) · [All framework recipes](https://frameworkrecipes.dev/)

**Framework Recipes**

Implementation, failure path, check.

Published by Perch · © 2026 Verglas LLC. Framework Recipes is a Perch publication.

Perch’s MIT license covers the CLI; hosted model analysis is a paid service. Relevant source is sent to the configured model service.

[Perch CLI MIT license](https://github.com/lakeday-org/perch/blob/main/LICENSE) · [Article text](https://frameworkrecipes.dev/problems/resource-cleanup/index.md) · [Resource index](https://frameworkrecipes.dev/llms.txt)

[Privacy](https://frameworkrecipes.dev/privacy/) ·
