From 3e029f46ffde9b97a9093774a13ebb714dea82d2 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 5 May 2026 19:47:45 -0700 Subject: [PATCH] fix: clean up benchmark resources before worker exit Assisted-by: openai:gpt-5.5 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> --- benchmarks/benchmark.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/benchmarks/benchmark.js b/benchmarks/benchmark.js index 5c8f49ea5c8..99be6808992 100644 --- a/benchmarks/benchmark.js +++ b/benchmarks/benchmark.js @@ -102,13 +102,30 @@ const dispatcher = new Class(httpBaseOptions.url, { ...dest }) -setGlobalDispatcher(new Agent({ +const globalDispatcher = new Agent({ pipelining, connections, connect: { rejectUnauthorized: false } -})) +}) + +setGlobalDispatcher(globalDispatcher) + +async function cleanup () { + httpNoKeepAliveOptions.agent.destroy() + httpKeepAliveOptions.agent.destroy() + axiosAgent.destroy() + fetchAgent.destroy() + gotAgent.destroy() + requestAgent.destroy() + superagentAgent.destroy() + + await Promise.allSettled([ + dispatcher.destroy(), + globalDispatcher.destroy() + ]) +} class SimpleRequest { constructor (resolve) { @@ -317,8 +334,16 @@ async function main () { // https://github.com/ladjs/superagent/issues/1540#issue-561464561 superagent = _superagent.agent().use((req) => req.agent(superagentAgent)) + // cronometro runs each benchmark in a worker, so attach cleanup to every + // test before the worker exits. + const tests = Object.fromEntries( + Object.entries(experiments).map(([name, test]) => { + return [name, { test, after: cleanup }] + }) + ) + cronometro( - experiments, + tests, { iterations, errorThreshold, @@ -330,7 +355,7 @@ async function main () { } printResults(results) - dispatcher.destroy() + cleanup() } ) }