Files
opencode/packages/opencode/test/memory/abort-leak-webfetch.ts
Dax Raad a9e8b7bdf7 core: isolate abort controller memory leak test in fresh process
The previous test was measuring heap growth while running inside the shared
Instance context, which included unrelated tool runtime state that could
obscure the actual abort controller leak signal.

Now the fetch operations run in a dedicated Bun worker process, giving a
clean baseline for memory measurement that accurately reflects whether
abort listeners are being properly cleaned up after timed fetches.
2026-04-09 22:47:16 -04:00

50 lines
952 B
TypeScript

import { abortAfterAny } from "../../src/util/abort"
const MB = 1024 * 1024
const ITERATIONS = 50
const heap = () => {
Bun.gc(true)
return process.memoryUsage().heapUsed / MB
}
const server = Bun.serve({
port: 0,
fetch() {
return new Response("hello from local", {
headers: {
"content-type": "text/plain",
},
})
},
})
const url = `http://127.0.0.1:${server.port}`
async function run() {
const { signal, clearTimeout } = abortAfterAny(30000, new AbortController().signal)
try {
const response = await fetch(url, { signal })
await response.text()
} finally {
clearTimeout()
}
}
try {
await run()
Bun.sleepSync(100)
const baseline = heap()
for (let i = 0; i < ITERATIONS; i++) {
await run()
}
Bun.sleepSync(100)
const after = heap()
process.stdout.write(JSON.stringify({ baseline, after, growth: after - baseline }))
} finally {
server.stop(true)
process.exit(0)
}