Skip to content

Commit c856e00

Browse files
only21milclaudemaria-rcks
authored
fix(desktop): stop the macOS Settings tracker when T3 exits (#22497)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent ee2800e commit c856e00

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎apps/desktop/src/permissions/MacSettingsWindow.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeEvents from "node:events";
2+
import * as NodeVM from "node:vm";
23
import { beforeEach, expect, it, vi } from "vite-plus/test";
34
import { watchMacSettingsWindow } from "./MacSettingsWindow.ts";
45

@@ -42,3 +43,29 @@ it("distinguishes unavailable tracking from a valid absent window", () => {
4243
expect(unavailable).toHaveBeenCalledTimes(2);
4344
stop();
4445
});
46+
it("polls while its parent lives and stops after reparenting to launchd", () => {
47+
watchMacSettingsWindow(vi.fn(), vi.fn())();
48+
const script = mocks.spawn.mock.lastCall?.[1][3];
49+
const imports = new Set<string>();
50+
const writeData = vi.fn();
51+
const poll = vi.fn(() => ({ count: 0 }));
52+
const sleep = vi.fn(() => {
53+
if (sleep.mock.calls.length > 2) throw new Error("tracker kept polling after parent exit");
54+
});
55+
const bridge = Object.assign((line: string) => ({ dataUsingEncoding: () => line }), {
56+
getppid: () => {
57+
if (!imports.has("unistd")) throw new Error("unistd bridge unavailable");
58+
return sleep.mock.calls.length < 2 ? 42 : 1;
59+
},
60+
NSRunningApplication: { runningApplicationsWithBundleIdentifier: poll },
61+
NSFileHandle: { fileHandleWithStandardOutput: { writeData } },
62+
NSThread: { sleepForTimeInterval: sleep },
63+
NSUTF8StringEncoding: 4,
64+
});
65+
NodeVM.runInNewContext(script + "\nrun();", {
66+
ObjC: { import: (name: string) => imports.add(name) },
67+
$: bridge,
68+
});
69+
expect(poll).toHaveBeenCalledTimes(2);
70+
expect(writeData.mock.calls).toEqual([["null\n"]]);
71+
});

‎apps/desktop/src/permissions/MacSettingsWindow.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const decodeSettingsWindow = Schema.decodeUnknownSync(Schema.fromJsonString(Sett
2020
const SETTINGS_WINDOW_SCRIPT = `
2121
ObjC.import("CoreGraphics");
2222
ObjC.import("AppKit");
23+
ObjC.import("unistd");
2324
function run() {
2425
let previous = "";
25-
while (true) {
26+
while ($.getppid() !== 1) {
2627
const apps = $.NSRunningApplication.runningApplicationsWithBundleIdentifier("com.apple.systempreferences");
2728
let result = null;
2829
if (apps.count > 0) {

0 commit comments

Comments
 (0)