Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/daemon/relay.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { setFlagsFromString } from "node:v8";
import { runInNewContext } from "node:vm";

import { describe, expect, it, vi } from "vitest";

import { LinkChannelError, type LinkChannel } from "../link/index.js";
Expand All @@ -23,6 +26,16 @@ const logger: Logger = {

type Input = Parameters<RelayConnection["request"]>[0];

// why: a leak is an object nothing can reach that a full collection still keeps; a timer turn between collections lets FinalizationRegistry callbacks run.
async function collectGarbage(): Promise<void> {
setFlagsFromString("--expose-gc");
const gc = runInNewContext("gc") as () => void;
for (let round = 0; round < 3; round += 1) {
await new Promise((resolve) => setTimeout(resolve, 10));
gc();
}
}

async function acceptedChannel() {
const pair = linkPair();
const opened = await pair.gateway.openChannel({ signal: new AbortController().signal });
Expand Down Expand Up @@ -207,4 +220,32 @@ describe("a worker's relay connection", () => {

expect(read.state).toBe("rejected");
});

it("lets every Request it handed a target be collected once the relay has answered, a GET with no body and a PUT with one", async () => {
const handed: WeakRef<Request>[] = [];
const target: RelayTarget = async (request) => {
handed.push(new WeakRef(request));
await request.arrayBuffer();
return new Response("ok");
};
const relays = new RelayConnection({ echo: target }, logger);
const push: RelayPush = { head: () => Promise.resolve() };
const outcomes = [];
for (const input of [relayInput(), relayInput({ hasBody: true, method: "PUT" })]) {
const pair = linkPair();
const opened = await pair.gateway.openChannel({ signal: new AbortController().signal });
const answered = relays.request(input as Input, push, pair.worker.acceptChannel(opened.id));
await opened.writable.getWriter().close();
void opened.readable.pipeTo(new WritableStream()).catch(() => undefined);
outcomes.push(await settleWithin(answered));
}

await collectGarbage();

expect(outcomes).toEqual([
{ state: "fulfilled", value: {} },
{ state: "fulfilled", value: {} },
]);
expect(handed.map((ref) => ref.deref() === undefined)).toEqual([true, true]);
});
});
2 changes: 2 additions & 0 deletions src/daemon/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export class RelayConnection {
return await this.#run(input, call, active, push);
} finally {
this.#active.delete(input.relayId);
// why: the Request carries the relay's own abort signal, whose listener closes over `active`; keeping the Request makes a cycle through fetch's finalizer registry that is never collected.
active.request = undefined;
}
}

Expand Down
Loading