360 Secure SkillHub
360 安全技能中心

Resource Leak

该技能说明资源泄漏的常见症状,如未关闭的文件、连接、监听器与定时器,并建议使用 with、defer、try/finally 等构造保证释放,记录所有权,复用连接池并测试清理路径。

mvogt99
mvogt99
热度 31.41
最近更新 2026-05-11 17:46:01
分类:开发依赖:纯提示词应用场景:代码审查 / 调试排错

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add resource-leak
Metadata
name
resource-leak
description
Files, sockets, subscriptions, or other finite resources are acquired without a guaranteed release path.
emoji
💧
metadata
{"clawdis":{"os":["macos","linux","windows"]}}

resource-leak

A resource is opened but not closed. Under low load the process recycles or the OS cleans up on exit, so the bug stays hidden. Under real load, file-descriptor exhaustion, memory bloat, or orphaned connections appear and cascade.

Symptoms

  • open(...) without a matching close() or with block.
  • Database connections or HTTP clients instantiated per-request without pooling or teardown.
  • Event listeners / observers / subscriptions registered but never removed.
  • Background tasks or timers spawned without cancellation paths.
  • Gradual memory growth that correlates with request count.

What to do

  • Pair every acquire with a release. Use language constructs that guarantee teardown: with (Python), using / try-with-resources (C#/Java), defer (Go), try/finally (JS/TS).
  • For resources that outlive a single function, document ownership: who closes it, when, on which code path — including error paths.
  • Prefer pooled clients (HTTP, database) over creating a new one per call.
  • When registering event listeners, return the unregister function in the same scope so the caller can clean up.
  • Test the teardown path. Kill a process or trigger an error mid-operation and confirm resources are released.