Skip to content

Commit 84dada7

Browse files
authored
fix(diagnostics): browser bridge keeps shared references in its payload (#3967)
`toSerializable` tracked every object it had written in one `seen` set, so a reference that appeared twice was dropped as if it were a cycle. A `LONG_HOLD` diagnostic carries the hold's own `blockers` and `acknowledgements` arrays in its `data`, and diagnostics are serialized before `attribution.holds`, so the transferred hold record lost both and `expectNoSilentHolds` threw a `TypeError` in `isSilent` on any browser capture with a long hold. The replacer now tracks only the ancestors of the value being written: a shared reference is written again, and only an object that contains itself is dropped. Fixes #3960
1 parent 15e1228 commit 84dada7

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/diagnostics": patch
3+
---
4+
5+
The browser bridge keeps shared references in the payload it transfers. `toSerializable` treated every object it had already written as a cycle and dropped it, so a `LONG_HOLD` diagnostic, whose `data` carries the hold's own `blockers` and `acknowledgements` arrays, left the hold record in `attribution.holds` without them, and `expectNoSilentHolds` threw a `TypeError` on any browser capture that recorded a long hold. The serializer now tracks only the ancestors of the value being written: an object reached twice is written twice, and only an object that contains itself is dropped.

‎packages/diagnostics/src/browser.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ export interface DiagnosticsBridge {
5757
* fail after a capture succeeded.
5858
*/
5959
function toSerializable<T>(value: T): T {
60-
const seen = new WeakSet<object>();
60+
// Only the ancestors of the current value: a shared reference is not a cycle.
61+
const ancestors: object[] = [];
6162
return JSON.parse(
62-
JSON.stringify(value, (_key, entry) => {
63+
JSON.stringify(value, function (this: object, _key, entry) {
6364
if (typeof entry === "function" || typeof entry === "symbol") return undefined;
6465
if (typeof entry === "bigint") return String(entry);
6566
if (typeof entry === "object" && entry !== null) {
66-
if (seen.has(entry)) return undefined;
67-
seen.add(entry);
67+
while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop();
68+
if (ancestors.includes(entry)) return undefined;
69+
ancestors.push(entry);
6870
}
6971
return entry;
7072
})

‎packages/diagnostics/tests/browser-bridge.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ describe("browser bridge + playwright adapter", () => {
150150
app.dispose();
151151
});
152152

153+
it("drops a cycle in a record without failing the capture", async () => {
154+
const realm: Record<string, unknown> = {};
155+
const bridge = installDiagnosticsBridge(realm);
156+
const channel = OBSERVE!.records as unknown as {
157+
emit(type: string, event: unknown, live: unknown): void;
158+
};
159+
const meta: Record<string, unknown> = {};
160+
meta.self = meta;
161+
162+
bridge.begin({ attribution: false });
163+
channel.emit("call", { id: "todos/list", at: 1, meta }, undefined);
164+
const payload = bridge.end();
165+
expect(payload.records.call).toEqual([{ id: "todos/list", at: 1, meta: {} }]);
166+
});
167+
153168
it("supports diagnostics-only captures", async () => {
154169
const realm: Record<string, unknown> = {};
155170
installDiagnosticsBridge(realm);

‎packages/diagnostics/tests/responsiveness.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isPending
1919
} from "@solidjs/signals";
2020
import {
21+
ARTIFACT_FORMAT_VERSION,
2122
artifactToJSONL,
2223
assertBudget,
2324
captureArtifact,
@@ -253,4 +254,28 @@ describe("browser bridge", () => {
253254
expect(payload.attribution!.feedback.sources).toHaveLength(1);
254255
feed.dispose();
255256
});
257+
258+
it("keeps a long hold's blockers and acknowledgements beside its LONG_HOLD diagnostic", async () => {
259+
const realm: Record<string, unknown> = {};
260+
const bridge = installDiagnosticsBridge(realm);
261+
262+
const feed = pagedFeed(true);
263+
await feed.load("a");
264+
bridge.begin({ attribution: { ...attribution, longHolds: { infoMs: 10, warnMs: 60_000 } } });
265+
feed.setPage(2);
266+
flush();
267+
await wait(30);
268+
await feed.load("b");
269+
270+
const payload = bridge.end();
271+
feed.dispose();
272+
expect(payload.diagnostics.map(d => d.code)).toContain("LONG_HOLD");
273+
expect(payload.attribution!.holds).toHaveLength(1);
274+
expect(payload.attribution!.holds[0]).toMatchObject({
275+
long: true,
276+
blockers: ["posts"],
277+
acknowledgements: [{ kind: "isPending", source: "posts" }]
278+
});
279+
expectNoSilentHolds({ formatVersion: ARTIFACT_FORMAT_VERSION, ...payload });
280+
});
256281
});

0 commit comments

Comments
 (0)