Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(ios-simulator): guard deferred H.264 pump restart against nulled controller (#4974)

billythegoda1f6ec94adfe
diff --git a/packages/ios-simulator-runtime/src/frame-pump.test.ts b/packages/ios-simulator-runtime/src/frame-pump.test.ts
index f7f3fbd81e..b085af8670 100644
--- a/packages/ios-simulator-runtime/src/frame-pump.test.ts
+++ b/packages/ios-simulator-runtime/src/frame-pump.test.ts
@@ -379,4 +379,66 @@ describe("IOSSimulatorH264FramePump", () => {
);
pump.clear("instance-a");
});
+
+ it("skips the queued restart when visibility flips off before the predecessor task settles", async () => {
+ const driver = nativeDriverWithStreams([]);
+ driver.streamNativeFrames.mockImplementation(
+ async ({ signal, onFrame }) => {
+ await onFrame(h264Frame(0));
+ await new Promise<void>((resolve) => {
+ signal?.addEventListener("abort", () => resolve(), { once: true });
+ });
+ return {
+ frameCount: 1,
+ byteCount: 6,
+ startedAt: "now",
+ firstFrameAt: "now",
+ endedAt: "now",
+ endReason: "aborted",
+ };
+ },
+ );
+ const rejections: unknown[] = [];
+ const onUnhandled = (reason: unknown) => rejections.push(reason);
+ process.on("unhandledRejection", onUnhandled);
+ try {
+ const pump = new IOSSimulatorH264FramePump();
+ const base = {
+ instanceId: "instance-a",
+ driver,
+ };
+ const profileA = {
+ encoding: "h264" as const,
+ framesPerSecond: 5,
+ scalingPercent: 50,
+ };
+ const profileB = {
+ encoding: "h264" as const,
+ framesPerSecond: 10,
+ scalingPercent: 70,
+ };
+ pump.setVisible({ ...base, generation: 1, profile: profileA, visible: true });
+ await vi.waitFor(() =>
+ expect(pump.snapshot("instance-a")?.latestFrame?.sequence).toBe(1),
+ );
+ // Profile change queues the restart behind the still-running generation-1
+ // task; hiding before that task settles nulls entry.controller while the
+ // deferred run() has not executed yet.
+ pump.setVisible({ ...base, generation: 2, profile: profileB, visible: true });
+ const paused = pump.setVisible({
+ ...base,
+ generation: 2,
+ profile: profileB,
+ visible: false,
+ });
+ expect(paused.state).toBe("paused");
+ await new Promise((resolve) => setTimeout(resolve, 10));
+
+ expect(rejections).toEqual([]);
+ expect(pump.snapshot("instance-a")?.state).toBe("paused");
+ pump.clear("instance-a");
+ } finally {
+ process.off("unhandledRejection", onUnhandled);
+ }
+ });
});
diff --git a/packages/ios-simulator-runtime/src/frame-pump.ts b/packages/ios-simulator-runtime/src/frame-pump.ts
index f4de28d59b..800b046ac2 100644
--- a/packages/ios-simulator-runtime/src/frame-pump.ts
+++ b/packages/ios-simulator-runtime/src/frame-pump.ts
@@ -321,8 +321,15 @@ export class IOSSimulatorH264FramePump {
if (!entry.task) {
entry.controller = new AbortController();
entry.state = "connecting";
- const run = () =>
- this.#run(input.instanceId, entry!, entry!.controller!.signal);
+ // The run is deferred behind the predecessor task, so the controller may
+ // have been aborted and nulled by a setVisible({ visible: false }) that
+ // arrives before the predecessor settles — dereferencing it there threw
+ // an unhandled TypeError rejection. Skip instead of running in that case.
+ const run = () => {
+ const signal = entry!.controller?.signal;
+ if (!signal || signal.aborted) return Promise.resolve();
+ return this.#run(input.instanceId, entry!, signal);
+ };
entry.task = (
predecessor ? predecessor.catch(() => undefined).then(run) : run()
).finally(() => {