Skip to content
Open
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
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function (config) {
customLaunchers: {
DockerChrome: {
base: "ChromeHeadless",
flags: ["--no-sandbox"],
flags: ["--no-sandbox", "--js-flags=--expose-gc"],
},
},
};
Expand Down
20 changes: 19 additions & 1 deletion src/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ type PendingListenersMap = Map<
type EndpointWithPendingListeners = {
endpoint: Endpoint;
pendingListeners: PendingListenersMap;
releaseDeferred?: boolean;
};

/**
Expand Down Expand Up @@ -401,6 +402,10 @@ function closeEndPoint(endpoint: Endpoint) {

export function wrap<T>(ep: Endpoint, target?: any): Remote<T> {
const pendingListeners : PendingListenersMap = new Map();
const epWithPendingListeners: EndpointWithPendingListeners = {
endpoint: ep,
pendingListeners,
};

ep.addEventListener("message", function handleMessage(ev: Event) {
const { data } = ev as MessageEvent;
Expand All @@ -416,10 +421,19 @@ export function wrap<T>(ep: Endpoint, target?: any): Remote<T> {
resolver(data);
} finally {
pendingListeners.delete(data.id);
if (
epWithPendingListeners.releaseDeferred &&
pendingListeners.size === 0
) {
epWithPendingListeners.releaseDeferred = false;
releaseEndpoint(epWithPendingListeners).finally(() => {
pendingListeners.clear();
});
}
}
});

return createProxy<T>({ endpoint: ep, pendingListeners }, [], target) as any;
return createProxy<T>(epWithPendingListeners, [], target) as any;
}

function throwIfProxyReleased(isReleased: boolean) {
Expand Down Expand Up @@ -455,6 +469,10 @@ const proxyFinalizers =
const newCount = (proxyCounter.get(epWithPendingListeners) || 0) - 1;
proxyCounter.set(epWithPendingListeners, newCount);
if (newCount === 0) {
if (epWithPendingListeners.pendingListeners.size > 0) {
epWithPendingListeners.releaseDeferred = true;
return;
}
releaseEndpoint(epWithPendingListeners).finally(() => {
epWithPendingListeners.pendingListeners.clear();
});
Expand Down
36 changes: 36 additions & 0 deletions tests/same_window.comlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,42 @@ describe("Comlink in the same realm", function () {
expect(finalized).to.be.true;
});

it("in-flight requests should not be dropped when proxy is GC'd mid-await", async function () {
if (typeof globalThis.gc !== "function") this.skip();
this.timeout(15000);

const hammer = setInterval(() => globalThis.gc(), 50);
const garbage = [];
const pressure = setInterval(() => {
garbage.push(new Uint8Array(2_000_000));
if (garbage.length > 20) garbage.length = 0;
}, 50);

try {
for (let i = 0; i < 20; i++) {
const { port1, port2 } = new MessageChannel();
port1.start();
port2.start();
Comlink.expose(
{
slow: () => new Promise((r) => setTimeout(() => r("ok"), 100)),
},
port2
);
const result = await Promise.race([
Comlink.wrap(port1).slow(),
new Promise((_, reject) =>
setTimeout(() => reject(new Error(`call ${i} hung — port closed mid-await`)), 2000)
),
]);
expect(result).to.equal("ok");
}
} finally {
clearInterval(hammer);
clearInterval(pressure);
}
});

// commented out this test as it could be unreliable in various browsers as
// it has to wait for GC to kick in which could happen at any timing
// this does seem to work when testing locally
Expand Down