fix(test): prevent memory and resource leaks in test infrastructure #9137
+218
−58
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Draft
Title: fix(test): prevent memory and resource leaks in test infrastructure
Summary
Fixes critical resource leaks in test infrastructure that were causing:
Changes
1. Re-enable Temp Directory Cleanup
File:
packages/opencode/test/fixture/fixture.tsconst result = { [Symbol.asyncDispose]: async () => { await options?.dispose?.(dirpath) - // await fs.rm(dirpath, { recursive: true, force: true }) + await fs.rm(dirpath, { recursive: true, force: true }) }, path: realpath, extra: extra as T, }Impact: Temp directories created by
tmpdir()fixture are now properly deleted when tests complete. Previously commented out, causing directories to accumulate in/tmp/forever.2. Add Process Cleanup Guarantees to LSP Tests
File:
packages/opencode/test/lsp/client.test.tsWrapped all LSP client tests with
try/finallyblocks:test("handles workspace/workspaceFolders request", async () => { const handle = spawnFakeServer() as any - const client = await Instance.provide({...}) - // ... test code ... - await client.shutdown() + try { + const client = await Instance.provide({...}) + // ... test code ... + await client.shutdown() + } finally { + handle.process.kill() + } })Impact: Fake LSP server processes are now always killed, even when tests throw exceptions before reaching
shutdown(). Previously, failing tests would leave orphan Node.js processes consuming memory.Motivation
While developing locally, terminals running OpenCode tests would "freeze" then close unexpectedly. Investigation revealed:
Evidence from System Logs
OOM Killer Activity
V8 Stack Traces
System State
Testing
Verify Temp Directory Cleanup
Expected: Minimal or no
opencode-test-*directories remaining.Verify Process Cleanup
Expected: No
fake-lsp-serverprocesses running after test failure.Verify Memory Stability in Watch Mode
Expected: Memory usage remains stable (doesn't grow indefinitely).
Impact
Related Issues
Fixes #9136
Checklist
bun testNotes
try/finallyorafterEachhooksFiles Changed
OpenCode Version
v1.1.24 (dev branch)
Environment