fix(system-proxy): wait out launchd throttle before re-registering drifted job

实测(BTM uuid 不变、runs 持续累加):注销后立即重注会命中 launchd 尚未移除
的旧 job 定义,SMAppService 照常返回成功但定义不刷新,EX_CONFIG 循环继续。
注销后等过 minimum runtime(10s)节流窗再重注,launchd 才导入新定义
(program identifier + parent bundle 形态)并以新定义拉起 daemon。间隔经
controller 注入,生产 15s、用例 10ms。

已在实机验证全链路:BundleProgram 定义导入 → launchd 按需拉起 root daemon →
客户端签名校验 → apply 写入 SystemConfiguration(scutil --proxy 呈现
SOCKS/HTTP/HTTPS 指向本地入口,PAC 与自动发现关闭)→ 应答返回 → 二次收敛走
等价零写入路径。
This commit is contained in:
Qiu Yuzhou
2026-09-30 19:15:06 +08:00
parent 3bb6b88fbb
commit ec92628939
3 changed files with 13 additions and 2 deletions
@@ -118,7 +118,7 @@ extension ProxyRuntimeController {
/// 后自动重写。收敛路径永不清理(issue #71)。
func convergeSystemProxy() async {
guard settings.systemProxyEnabled else { return }
guard ensureHelperAvailableForApply() else {
guard await ensureHelperAvailableForApply() else {
systemProxyState = .pending
return
}
@@ -146,12 +146,16 @@ extension ProxyRuntimeController {
/// 与上次注册时的指纹漂移(app 更新改过清单)才注销重注刷新 launchd 的
/// job 定义;未注册则尝试注册(注册本身不弹授权框,成功即记指纹);待批准
/// 或注册失败时置位批准路径并保持待应用。
private func ensureHelperAvailableForApply() -> Bool {
private func ensureHelperAvailableForApply() async -> Bool {
switch systemProxyHelper.status {
case .approved:
if Self.helperRegistrationDrifted() {
do {
try systemProxyHelper.unregister()
// launchd 对节流中(spawn scheduled)job 的移除是异步的:立即重注
// 会命中尚未移除的旧定义(实测注销重注后定义不变)。等过 minimum
// runtime(10s)节流窗再重注;间隔内收敛以旧定义失败呈超时。
try? await Task.sleep(nanoseconds: helperRefreshDelayNanoseconds)
try systemProxyHelper.register()
Self.storeHelperRegistrationStamp()
} catch {
@@ -96,6 +96,8 @@ final class ProxyRuntimeController: ObservableObject {
let firewallExecutableURLs: [URL]
let firewallPollIntervalNanoseconds: UInt64
let launchHealthTimeoutSeconds: TimeInterval
/// 注册清单漂移重注时,注销与重注的间隔(launchd 对节流中 job 的移除异步)。
let helperRefreshDelayNanoseconds: UInt64
/// 信号发送缝(默认 kill),单测观测 SIGUSR1 投递。
let sendSignal: @Sendable (Int32, Int32) -> Int32
let processIsAlive: @Sendable (Int32) -> Bool
@@ -136,6 +138,7 @@ final class ProxyRuntimeController: ObservableObject {
firewallExecutableURLs: [URL]? = nil,
firewallPollIntervalNanoseconds: UInt64 = 2_000_000_000,
launchHealthTimeoutSeconds: TimeInterval = 15,
helperRefreshDelayNanoseconds: UInt64 = 15_000_000_000,
sendSignal: @escaping @Sendable (Int32, Int32) -> Int32 = { kill($0, $1) },
processIsAlive: @escaping @Sendable (Int32) -> Bool = { $0 > 0 && kill($0, 0) == 0 }
) {
@@ -166,6 +169,7 @@ final class ProxyRuntimeController: ObservableObject {
self.firewallExecutableURLs = firewallExecutableURLs ?? Self.defaultFirewallExecutableURLs
self.firewallPollIntervalNanoseconds = firewallPollIntervalNanoseconds
self.launchHealthTimeoutSeconds = launchHealthTimeoutSeconds
self.helperRefreshDelayNanoseconds = helperRefreshDelayNanoseconds
self.sendSignal = sendSignal
self.processIsAlive = processIsAlive
let persistedTarget = try? activationFileStore.loadActiveTargetID()
@@ -94,6 +94,8 @@ final class ProxyRuntimeControllerTests: XCTestCase {
firewallPollIntervalNanoseconds: UInt64 = 1_000_000,
// 生产默认 15 秒健康窗;FakeProbe 结果确定,短窗口走同一超时呈现路径。
launchHealthTimeoutSeconds: TimeInterval = 0.05,
// 漂移重注的注销-重注间隔与用例无关,缩短保持套件快速。
helperRefreshDelayNanoseconds: UInt64 = 10_000_000,
processIsAlive: @escaping @Sendable (Int32) -> Bool = { $0 == 42 },
customRuleStore: CustomRuleStore? = nil
) -> ProxyRuntimeController {
@@ -135,6 +137,7 @@ final class ProxyRuntimeControllerTests: XCTestCase {
firewallExecutableURLs: firewallExecutableURLs,
firewallPollIntervalNanoseconds: firewallPollIntervalNanoseconds,
launchHealthTimeoutSeconds: launchHealthTimeoutSeconds,
helperRefreshDelayNanoseconds: helperRefreshDelayNanoseconds,
sendSignal: { [signals] pid, number in signals!.send(pid, number) },
processIsAlive: processIsAlive)
}