find-resource-leaks
Detects resource leaks in PHP code. Finds unclosed file handles, database connections not released, streams not freed, missing finally blocks, temporary files not cleaned.
Best use case
find-resource-leaks is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Detects resource leaks in PHP code. Finds unclosed file handles, database connections not released, streams not freed, missing finally blocks, temporary files not cleaned.
Teams using find-resource-leaks should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/find-resource-leaks/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How find-resource-leaks Compares
| Feature / Agent | find-resource-leaks | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Detects resource leaks in PHP code. Finds unclosed file handles, database connections not released, streams not freed, missing finally blocks, temporary files not cleaned.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Resource Leak Detection
Analyze PHP code for resource leak issues.
## Detection Patterns
### 1. Unclosed File Handles
```php
// BUG: File handle not closed
$file = fopen('data.txt', 'r');
$content = fread($file, filesize('data.txt'));
// Missing: fclose($file);
// BUG: Early return without close
$file = fopen($path, 'w');
if (!$valid) {
return false; // File handle leaked
}
fwrite($file, $data);
fclose($file);
```
### 2. Database Connections Not Released
```php
// BUG: Connection not closed
$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->query($sql);
// PDO not unset, may hold connection
// BUG: In connection pool context
$connection = $pool->acquire();
$result = $connection->query($sql);
if ($error) {
throw new Exception(); // Connection leaked
}
$pool->release($connection);
```
### 3. Streams Not Freed
```php
// BUG: Stream not closed
$stream = fopen('php://temp', 'r+');
fwrite($stream, $data);
// Missing: fclose($stream);
// BUG: cURL handle not closed
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Missing: curl_close($ch);
```
### 4. Missing Finally Blocks
```php
// BUG: No finally for cleanup
$lock = $this->acquireLock();
try {
$this->process();
} catch (Exception $e) {
$this->log($e);
throw $e; // Lock not released on exception
}
$this->releaseLock($lock);
// FIXED:
$lock = $this->acquireLock();
try {
$this->process();
} finally {
$this->releaseLock($lock);
}
```
### 5. Temporary Files Not Cleaned
```php
// BUG: Temp file not deleted
$tempFile = tempnam(sys_get_temp_dir(), 'prefix_');
file_put_contents($tempFile, $data);
$result = $this->processFile($tempFile);
// Missing: unlink($tempFile);
// BUG: Early return leaves temp file
$temp = tmpfile();
if (!$valid) {
return null; // Temp file not cleaned
}
```
### 6. Socket/Network Resources
```php
// BUG: Socket not closed
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $host, $port);
$data = socket_read($socket, 1024);
// Missing: socket_close($socket);
// BUG: SSH connection not closed
$connection = ssh2_connect($host);
ssh2_auth_password($connection, $user, $pass);
$stream = ssh2_exec($connection, $command);
// Resources not freed
```
### 7. Image/GD Resources
```php
// BUG: Image resource not destroyed
$image = imagecreatefromjpeg($path);
$resized = imagescale($image, 100, 100);
imagejpeg($resized, $output);
// Missing: imagedestroy($image); imagedestroy($resized);
```
### 8. Doctrine/ORM Resources
```php
// BUG: Large result set in memory
$query = $em->createQuery('SELECT u FROM User u');
$users = $query->getResult(); // All in memory
// BUG: Detached entities not cleared
foreach ($largeDataset as $data) {
$entity = new Entity($data);
$em->persist($entity);
$em->flush();
// Missing: $em->clear(); for large batches
}
```
## Grep Patterns
```bash
# fopen without fclose
Grep: "fopen\([^)]+\)" --glob "**/*.php"
Grep: "fclose\(" --glob "**/*.php"
# curl_init without curl_close
Grep: "curl_init\(" --glob "**/*.php"
Grep: "curl_close\(" --glob "**/*.php"
# try without finally
Grep: "try\s*\{[^}]+\}\s*catch" --glob "**/*.php"
# tempnam/tmpfile usage
Grep: "(tempnam|tmpfile)\(" --glob "**/*.php"
```
## Severity Classification
| Pattern | Severity |
|---------|----------|
| Database connection leak | 🔴 Critical |
| File handle in loop | 🔴 Critical |
| Lock not released | 🔴 Critical |
| Single file handle | 🟠 Major |
| Temp file not cleaned | 🟠 Major |
| Image resource | 🟡 Minor |
## Best Practices
### Use try-finally
```php
$resource = acquire();
try {
process($resource);
} finally {
release($resource);
}
```
### Use Context Managers (PHP 8.1+)
```php
// Custom destructor-based cleanup
$file = new FileHandler($path);
// Automatically closed on unset/scope exit
```
### Use Transactions Properly
```php
$connection->beginTransaction();
try {
$this->process();
$connection->commit();
} catch (Throwable $e) {
$connection->rollBack();
throw $e;
}
```
## Output Format
```markdown
### Resource Leak: [Description]
**Severity:** 🔴/🟠/🟡
**Location:** `file.php:line`
**Type:** [File Handle|Connection|Stream|Lock|Temp File|...]
**Issue:**
Resource acquired but not properly released.
**Code:**
```php
// Problematic code
```
**Fix:**
```php
// With proper cleanup
```
```Related Skills
optimize-docker-compose-resources
Optimizes Docker Compose resource allocation for PHP stacks. Configures memory limits, CPU constraints, and service scaling.
find-type-issues
Detects type issues in PHP code. Finds implicit type coercion, mixed types in comparisons, unsafe casting, type mismatches in returns.
find-race-conditions
Detects race conditions in PHP code. Finds shared mutable state, check-then-act patterns, TOCTOU vulnerabilities, concurrent modification issues.
find-null-pointer-issues
Detects null pointer issues in PHP code. Finds property/method access on null, missing null checks, nullable returns without handling, optional chaining gaps.
find-logic-errors
Detects logic errors in PHP code. Finds incorrect conditions, wrong operators, missing switch cases, inverted logic, short-circuit evaluation issues.
find-infinite-loops
Detects infinite loop risks in PHP code. Finds missing break conditions, incorrect loop variables, unbounded recursion, circular references.
find-exception-issues
Detects exception handling issues in PHP code. Finds swallowed exceptions, generic catches, missing exception handling, re-throwing without context, exception in finally.
find-boundary-issues
Detects boundary issues in PHP code. Finds array index out of bounds, empty collection access, off-by-one errors, integer overflow, string length issues.
bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
yii-knowledge
Yii framework knowledge base. Provides Yii3 modular architecture, DDD integration, PSR-7/PSR-15 compliance, persistence, DI, security (RBAC, auth), event system (PSR-14), queue/jobs, infrastructure components (cache, rate limiter, HTTP client), testing, and antipatterns for Yii PHP projects.
troubleshooting-template
Generates troubleshooting guides and FAQ sections for PHP projects. Creates problem-solution documentation.
trace-request-lifecycle
Traces full request lifecycle from Router through Middleware, Controller, UseCase, Repository to Response. Documents HTTP methods, routes, middleware stack, response codes, and error handling paths.