mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
fix(desktop): install IPC probe as data property to preserve accessor bridges
The invoke probe read internals.invoke then reassigned it via internals.invoke = wrapper. The terminal E2E backend defines invoke as an accessor whose setter reassigns the dispatcher's fallback closure, so the assignment routed the bridge's fallback back into the wrapper and overflowed the stack on the first non-terminal invoke, taking down app startup under the harness. Redefine invoke as a data property via Object.defineProperty (never the setter) and extract the logic to an import-light ipc.ts so it is unit-testable against the real bridge descriptor shape. Co-authored-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
DRIFT_THRESHOLD_MS,
|
||||
nextStallSample,
|
||||
} from "@/shared/profiling/drift.ts";
|
||||
import { installInvokeProbe } from "@/shared/profiling/ipc.ts";
|
||||
import { ProfileRecorder, RING_CAPACITY } from "@/shared/profiling/recorder.ts";
|
||||
|
||||
function fixedClock() {
|
||||
@@ -92,6 +93,89 @@ describe("classifyInput", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("installInvokeProbe", () => {
|
||||
it("returns false and stays passive when no invoke bridge exists", () => {
|
||||
assert.equal(
|
||||
installInvokeProbe(undefined, new Map(), () => {}),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
installInvokeProbe({ invoke: 42 }, new Map(), () => {}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("records a resolved invoke once without recursing", async () => {
|
||||
const calls = [];
|
||||
const internals = {
|
||||
invoke: (cmd) => Promise.resolve(`ok:${cmd}`),
|
||||
};
|
||||
let t = 0;
|
||||
assert.equal(
|
||||
installInvokeProbe(
|
||||
internals,
|
||||
new Map(),
|
||||
(cmd, dur, ok) => calls.push({ cmd, dur, ok }),
|
||||
() => (t += 5),
|
||||
),
|
||||
true,
|
||||
);
|
||||
const result = await internals.invoke("ping");
|
||||
assert.equal(result, "ok:ping");
|
||||
assert.deepEqual(calls, [{ cmd: "ping", dur: 5, ok: true }]);
|
||||
});
|
||||
|
||||
it("records a rejected invoke and re-throws without leaking pending", async () => {
|
||||
const calls = [];
|
||||
const pending = new Map();
|
||||
const internals = { invoke: () => Promise.reject(new Error("boom")) };
|
||||
installInvokeProbe(internals, pending, (cmd, _dur, ok) =>
|
||||
calls.push({ cmd, ok }),
|
||||
);
|
||||
await assert.rejects(() => internals.invoke("bad"), /boom/);
|
||||
assert.deepEqual(calls, [{ cmd: "bad", ok: false }]);
|
||||
assert.equal(pending.size, 0);
|
||||
});
|
||||
|
||||
it("preserves an accessor-backed reassignment bridge without stack overflow", async () => {
|
||||
// Reproduces the terminal E2E backend descriptor shape: `invoke` is an
|
||||
// accessor whose getter returns a dispatcher delegating unknown commands to
|
||||
// closure `inner`, and whose setter *reassigns* `inner`. A plain
|
||||
// `internals.invoke = wrapper` would route the dispatcher's fallback back
|
||||
// into the wrapper and overflow the stack on the first non-terminal call.
|
||||
const internals = {};
|
||||
let inner = null;
|
||||
Object.defineProperty(internals, "invoke", {
|
||||
configurable: true,
|
||||
get: () => (cmd, args, opts) => {
|
||||
if (cmd === "terminal_input") return Promise.resolve("term");
|
||||
if (!inner) throw new Error(`no mock bridge for ${cmd}`);
|
||||
return inner(cmd, args, opts);
|
||||
},
|
||||
set: (fn) => {
|
||||
inner = fn;
|
||||
},
|
||||
});
|
||||
// The real mock bridge is trapped through the setter after the harness runs.
|
||||
const realBridge = (cmd) => Promise.resolve(`real:${cmd}`);
|
||||
|
||||
const calls = [];
|
||||
assert.equal(
|
||||
installInvokeProbe(internals, new Map(), (cmd, _dur, ok) =>
|
||||
calls.push({ cmd, ok }),
|
||||
),
|
||||
true,
|
||||
);
|
||||
// App code trapping the invoke after the harness (as mockIPC does) must not
|
||||
// clobber the probe: the harness redefined `invoke` as a data property.
|
||||
inner = realBridge;
|
||||
|
||||
const result = await internals.invoke("non_terminal");
|
||||
assert.equal(result, "real:non_terminal");
|
||||
assert.deepEqual(calls, [{ cmd: "non_terminal", ok: true }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ProfileRecorder ring buffer", () => {
|
||||
it("bounds the buffer at RING_CAPACITY, dropping oldest", async () => {
|
||||
const rec = new ProfileRecorder("session-1", async () => {}, fixedClock());
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
// `<app-data>/profiling/session-<ts>.jsonl`. Four probes:
|
||||
// 1. main-thread stalls (timer drift, foreground-only) + input latency
|
||||
// (receipt → next paint, with pre-dispatch queue delay split out)
|
||||
// 2. Tauri IPC duration/outcome + a >10s pending-invoke watchdog (deadlocks)
|
||||
// 2. Tauri IPC duration/outcome + a >10s pending-invoke watchdog (deadlocks).
|
||||
// The invoke bridge is redefined as a data property (see ipc.ts) so an
|
||||
// accessor-backed bridge is preserved without looping into the wrapper.
|
||||
// 3. relay REQ→EOSE (history fetch) and publish send→ack round-trips
|
||||
// 4. periodic accumulator census (observer store, query cache, DOM nodes)
|
||||
//
|
||||
@@ -22,14 +24,18 @@ import {
|
||||
classifyInput,
|
||||
nextStallSample,
|
||||
} from "@/shared/profiling/drift";
|
||||
import {
|
||||
PENDING_INVOKE_MS,
|
||||
PENDING_SCAN_MS,
|
||||
type PendingInvoke,
|
||||
installInvokeProbe,
|
||||
} from "@/shared/profiling/ipc";
|
||||
import {
|
||||
FLUSH_INTERVAL_MS,
|
||||
ProfileRecorder,
|
||||
} from "@/shared/profiling/recorder";
|
||||
|
||||
const CENSUS_INTERVAL_MS = 120_000;
|
||||
const PENDING_INVOKE_MS = 10_000;
|
||||
const PENDING_SCAN_MS = 5_000;
|
||||
|
||||
// RelayClient methods whose resolution marks a REQ→EOSE round-trip (history
|
||||
// fetches resolve when the relay sends EOSE) versus a publish send→ack
|
||||
@@ -98,33 +104,14 @@ function installIpcProbe(rec: ProfileRecorder): void {
|
||||
const internals = (
|
||||
window as unknown as { __TAURI_INTERNALS__?: TauriInternals }
|
||||
).__TAURI_INTERNALS__;
|
||||
if (!internals || typeof internals.invoke !== "function") {
|
||||
// Track outstanding invokes so a hung command surfaces via the watchdog.
|
||||
const pending = new Map<number, PendingInvoke>();
|
||||
const installed = installInvokeProbe(internals, pending, (cmd, dur, ok) =>
|
||||
rec.record({ type: "ipc", cmd, dur, ok }),
|
||||
);
|
||||
if (!installed) {
|
||||
return;
|
||||
}
|
||||
const original = internals.invoke.bind(internals);
|
||||
// Track outstanding invokes so a hung command surfaces via the watchdog.
|
||||
const pending = new Map<number, { cmd: string; startedAt: number }>();
|
||||
let seq = 0;
|
||||
|
||||
internals.invoke = (cmd: string, args?: unknown, opts?: unknown) => {
|
||||
const id = seq++;
|
||||
const startedAt = performance.now();
|
||||
pending.set(id, { cmd, startedAt });
|
||||
const settle = (ok: boolean) => {
|
||||
pending.delete(id);
|
||||
rec.record({ type: "ipc", cmd, dur: performance.now() - startedAt, ok });
|
||||
};
|
||||
return original(cmd, args, opts).then(
|
||||
(value) => {
|
||||
settle(true);
|
||||
return value;
|
||||
},
|
||||
(error) => {
|
||||
settle(false);
|
||||
throw error;
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
window.setInterval(() => {
|
||||
const now = performance.now();
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
// Temporary renderer profiling harness (never merges) — IPC invoke probe.
|
||||
//
|
||||
// The interception logic lives here (not harness.ts) so it can be unit-tested
|
||||
// without harness.ts's module-load Tauri/store imports, matching drift.ts.
|
||||
|
||||
/** A command outstanding >10s is logged with its age — the deadlock detector. */
|
||||
export const PENDING_INVOKE_MS = 10_000;
|
||||
/** Watchdog scan cadence. */
|
||||
export const PENDING_SCAN_MS = 5_000;
|
||||
|
||||
export type InvokeFn = (
|
||||
cmd: string,
|
||||
args?: unknown,
|
||||
opts?: unknown,
|
||||
) => Promise<unknown>;
|
||||
|
||||
export type IpcInternals = { invoke: InvokeFn };
|
||||
|
||||
export type PendingInvoke = { cmd: string; startedAt: number };
|
||||
|
||||
/**
|
||||
* Wrap `__TAURI_INTERNALS__.invoke` to record each call's duration/outcome,
|
||||
* tracking outstanding calls in `pending` for the watchdog. Returns `false`
|
||||
* when no invoke bridge is present (the harness stays passive).
|
||||
*
|
||||
* The wrapper is installed as a **data property** via `Object.defineProperty`,
|
||||
* never `internals.invoke = wrapper`. The terminal E2E backend defines `invoke`
|
||||
* as an accessor whose getter returns a dispatcher that falls back to a closure
|
||||
* variable and whose setter *reassigns that fallback*; a plain assignment would
|
||||
* therefore route the bridge's fallback back into this wrapper and overflow the
|
||||
* stack on the first non-terminal invoke. Reading the getter once (to bind the
|
||||
* current implementation) and redefining `invoke` as a value preserves any
|
||||
* accessor- or reassignment-backed bridge without looping into ourselves.
|
||||
*/
|
||||
export function installInvokeProbe(
|
||||
internals: IpcInternals | undefined,
|
||||
pending: Map<number, PendingInvoke>,
|
||||
record: (cmd: string, dur: number, ok: boolean) => void,
|
||||
now: () => number = () => performance.now(),
|
||||
): boolean {
|
||||
if (!internals || typeof internals.invoke !== "function") {
|
||||
return false;
|
||||
}
|
||||
const original = internals.invoke.bind(internals);
|
||||
let seq = 0;
|
||||
|
||||
const wrapper: InvokeFn = (cmd, args, opts) => {
|
||||
const id = seq++;
|
||||
const startedAt = now();
|
||||
pending.set(id, { cmd, startedAt });
|
||||
const settle = (ok: boolean) => {
|
||||
pending.delete(id);
|
||||
record(cmd, now() - startedAt, ok);
|
||||
};
|
||||
return original(cmd, args, opts).then(
|
||||
(value) => {
|
||||
settle(true);
|
||||
return value;
|
||||
},
|
||||
(error) => {
|
||||
settle(false);
|
||||
throw error;
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
Object.defineProperty(internals, "invoke", {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: wrapper,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user