Problem
Multiple CLI commands open database connections without guaranteed cleanup via finally blocks. Additionally, a stream reader in url-fetcher is not properly cancelled on errors.
Severity: Major
Affected Files
CLI database leaks (src/cli/index.ts)
Commands that call initializeApp() or initializeAppWithEmbedding() without wrapping in try-finally with closeDatabase():
| Command |
Approximate Line |
pack remove |
1312 |
pack list (installed path) |
1334 |
pack create |
1365 |
connect obsidian |
1545 |
disconnect obsidian |
1727 |
disconnect notion |
1795 |
Stream reader leak (src/core/url-fetcher.ts)
| File |
Lines |
Issue |
src/core/url-fetcher.ts |
117-148 |
reader from response.body.getReader() not cancelled in finally block — only cancelled on size limit exceeded |
Implementation Plan
1. Add try-finally to all CLI commands
For each affected command, wrap the body in try-finally:
const { db } = initializeApp();
try {
// existing command body
} finally {
closeDatabase();
}
2. Fix stream reader cleanup
url-fetcher.ts lines 117-148: Wrap reader usage in try-finally:
const reader = response.body.getReader();
try {
let result = await reader.read();
while (!result.done) {
totalBytes += result.value.byteLength;
chunks.push(result.value);
if (totalBytes > maxBytes) {
break; // reader.cancel() handled in finally
}
result = await reader.read();
}
} finally {
await reader.cancel();
}
3. Tests
- Verify closeDatabase is called on command error paths (check with mock)
- Test url-fetcher reader cancellation on error