Skip to content

Commit a8d28b3

Browse files
domeniccodex
andauthored
Release windows retained by stylesheet parsing
css-tree retains the last onParseError callback, keeping its stylesheet's window reachable after window.close(). Forward parse errors through a callback whose reference to the original handler is cleared on both normal and exceptional exits. The upstream fix is tracked in csstree/csstree#384. Fixes #4339. Closes #4368 by being a better version of it that doesn't monkey patch a third-party library. Co-authored-by: codex <[email protected]>
1 parent 05a2c01 commit a8d28b3

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎lib/jsdom/living/css/helpers/css-parser.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ function parseIntoStyleSheet(cssText, globalObject, sheetImpl, onError) {
6363
try {
6464
ast = csstree.parse(cssText, {
6565
...STYLESHEET_PARSE_OPTIONS,
66-
onParseError: onError
66+
onParseError: onError ? error => onError(error) : undefined
6767
});
6868
} catch (e) {
6969
if (onError) {
7070
onError(e);
7171
}
7272
return;
73+
} finally {
74+
// css-tree retains its last `onParseError` callback, so release the reference to the caller's callback.
75+
// Upstream fix: https://github.com/csstree/csstree/pull/384
76+
onError = undefined;
7377
}
7478

7579
for (const node of ast.children) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import assert from "node:assert/strict";
2+
import { setImmediate } from "node:timers/promises";
3+
import { JSDOM, VirtualConsole } from "../../../lib/api.js";
4+
5+
const scenario = process.argv[2];
6+
const cssText = scenario === "valid" ? "p { color: orange; }" : "p { color: orange; } }";
7+
8+
async function createAndClose() {
9+
const virtualConsole = new VirtualConsole();
10+
const { window } = new JSDOM("", {
11+
virtualConsole,
12+
resources: scenario === "imported" ? "usable" : undefined
13+
});
14+
const ref = new WeakRef(window);
15+
const callbackError = new Error("The CSS error listener threw");
16+
let errorCount = 0;
17+
virtualConsole.on("jsdomError", error => {
18+
assert.equal(error.type, "css-parsing");
19+
assert.equal(error.sheetText, cssText);
20+
++errorCount;
21+
if (scenario === "throwing") {
22+
throw callbackError;
23+
}
24+
});
25+
26+
const style = window.document.createElement("style");
27+
style.textContent = scenario === "imported" ? `@import "data:text/css,${encodeURIComponent(cssText)}";` : cssText;
28+
if (scenario === "throwing") {
29+
assert.throws(() => window.document.head.append(style), error => error === callbackError);
30+
} else {
31+
window.document.head.append(style);
32+
if (scenario === "imported") {
33+
await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
34+
}
35+
const sheet = scenario === "imported" ? style.sheet.cssRules[0].styleSheet : style.sheet;
36+
assert.equal(sheet.cssRules.length, 1);
37+
}
38+
assert.equal(errorCount > 0, scenario !== "valid");
39+
40+
window.close();
41+
return ref;
42+
}
43+
44+
const windows = [];
45+
// Repeated values hit the property cache, avoiding another parse that would clear the retained callback.
46+
for (let i = 0; i < 10; ++i) {
47+
windows.push(await createAndClose());
48+
}
49+
50+
for (let i = 0; i < 10; ++i) {
51+
await setImmediate();
52+
global.gc();
53+
if (windows.every(ref => ref.deref() === undefined)) {
54+
break;
55+
}
56+
}
57+
58+
assert.deepEqual(windows.flatMap((ref, i) => ref.deref() === undefined ? [] : [i]), []);
59+
console.log("collected");

‎test/api/from-outside.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ describe("Test cases only possible to test from the outside", () => {
131131
assert.equal(stdout.trim(), "collected");
132132
});
133133

134+
for (const scenario of ["valid", "invalid", "throwing", "imported"]) {
135+
it(`does not retain closed windows after stylesheet parsing (${scenario})`, { timeout: 5000 }, () => {
136+
const fixturePath = path.resolve(__dirname, "./fixtures/stylesheet-parsing-with-gc.mjs");
137+
const { status, stderr, stdout } = spawnSync(process.execPath, ["--expose-gc", fixturePath, scenario], {
138+
encoding: "utf-8"
139+
});
140+
141+
assert.equal(status, 0, stderr);
142+
assert.equal(stdout.trim(), "collected");
143+
});
144+
}
145+
134146
it("does not dispatch storage events to removed iframe windows", async () => {
135147
const dom = new JSDOM("", { url: "https://example.com/" });
136148
const removedFrame = dom.window.document.createElement("iframe");

0 commit comments

Comments
 (0)