Skip to content

Commit 5385c2a

Browse files
committed
fix(ios): take the system-modal probe's target identity on main
The coordinate tap resolved its system-modal routing on the command queue and armed an abandoned probe's penalty with `currentBundleId` read there, while `applyMainOwnedSnapshotState` and the lifecycle code write that identity through `DispatchQueue.main.async`. A tap could penalize a target main was still clearing or rebinding. The probe now takes a `SnapshotProbePenaltyTarget`: a capture hands over the identity it already took on main, and the tap route hands over nothing and lets the probe's own main-side block capture the identity main holds once its work starts. No off-main path reads target identity any more.
1 parent ffc8549 commit 5385c2a

5 files changed

Lines changed: 82 additions & 9 deletions

File tree

‎.github/workflows/ios.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ jobs:
257257
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testHungCustomActionReadIsContainedAndRecovers \
258258
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAbandonedTreeCaptureSkipsQuerySweepAndHonorsWarmupExemption \
259259
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testNonInteractiveQuerySweepStopsAtTheSliceItsCallerWaitsFor \
260+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testQuerySweepThatEndsOnItsSliceDeadlinePenalizesChannelAndReachesPrivateAX \
261+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testCoordinateTapRoutingPenalizesTheIdentityMainSettledOnWhileTheWriteWasPending \
260262
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testBlockingModalSnapshotLeavesWarmupExemptionForTheFirstCapturePlan \
261263
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testPrivateAXPinnedRegularDepthReachesAcquisitionAndPresentation 2>&1 | tee /tmp/agent-device-runner-regressions.log
262264
node --input-type=module -e '

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandExecution.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2572,9 +2572,13 @@ extension RunnerTests {
25722572
}
25732573
#endif
25742574
let probeDeadline = Date().addingTimeInterval(systemModalProbeBudget)
2575+
// Routing runs on the command queue, so this hands the probe a target rather than a bundle id it
2576+
// read across the main boundary: the penalty an abandoned probe arms carries the identity main
2577+
// holds once the probe starts, not one whose write was still queued behind the block that
2578+
// occupied main (#2781).
25752579
return boundedBlockingSystemAlertSnapshot(
25762580
deadline: probeDeadline,
2577-
penaltyBundleId: currentBundleId
2581+
penaltyTarget: .mainOwnedTarget
25782582
) != nil
25792583
#else
25802584
return false

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ extension RunnerTests {
163163
let deadline = Date().addingTimeInterval(Self.snapshotPlanBudget)
164164
if let blocking = boundedBlockingSystemAlertSnapshot(
165165
deadline: deadline,
166-
penaltyBundleId: target.bundleId
166+
penaltyTarget: .prepared(bundleId: target.bundleId)
167167
) {
168168
return blocking
169169
}
@@ -288,7 +288,7 @@ extension RunnerTests {
288288
let deadline = Date().addingTimeInterval(Self.snapshotPlanBudget)
289289
if let blocking = boundedBlockingSystemAlertSnapshot(
290290
deadline: deadline,
291-
penaltyBundleId: target.bundleId
291+
penaltyTarget: .prepared(bundleId: target.bundleId)
292292
) {
293293
return blocking
294294
}
@@ -303,11 +303,14 @@ extension RunnerTests {
303303

304304
/// Runs the pre-plan SpringBoard system-modal probe as a bounded capture tier sharing the plan
305305
/// deadline, so a slow alert enumeration cannot bypass the snapshot timeout and stall (#1244).
306-
/// An abandoned probe penalizes the XCTest channel for `penaltyBundleId`.
307-
func boundedBlockingSystemAlertSnapshot(deadline: Date, penaltyBundleId: String?) -> DataPayload? {
306+
/// An abandoned probe penalizes the XCTest channel for `penaltyTarget`.
307+
func boundedBlockingSystemAlertSnapshot(
308+
deadline: Date,
309+
penaltyTarget: SnapshotProbePenaltyTarget
310+
) -> DataPayload? {
308311
boundedBlockingSystemAlertSnapshotBody(
309312
deadline: deadline,
310-
penaltyBundleId: penaltyBundleId
313+
penaltyTarget: penaltyTarget
311314
) { probeDeadline in
312315
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
313316
if let override = self.systemModalProbeOverrideForTesting {
@@ -325,7 +328,7 @@ extension RunnerTests {
325328
/// production runs and what the unit tests exercise.
326329
private func boundedBlockingSystemAlertSnapshotBody(
327330
deadline: Date,
328-
penaltyBundleId: String?,
331+
penaltyTarget: SnapshotProbePenaltyTarget,
329332
probe: @escaping (Date) -> DataPayload?
330333
) -> DataPayload? {
331334
#if os(macOS)
@@ -341,6 +344,7 @@ extension RunnerTests {
341344
}
342345
let probeDeadline = Date().addingTimeInterval(slice)
343346
let startedAt = Date()
347+
let penaltyIdentity = SnapshotProbePenaltyIdentity(penaltyTarget)
344348
do {
345349
return try runMainThreadWork(
346350
"system_modal_probe",
@@ -354,12 +358,13 @@ extension RunnerTests {
354358
},
355359
onAbandoned: {
356360
self.penalizeSnapshotXCTestChannel(
357-
bundleId: penaltyBundleId,
361+
bundleId: penaltyIdentity.penalizedBundleId,
358362
reason: "system_modal_probe_timeout"
359363
)
360364
}
361365
) {
362-
probe(probeDeadline)
366+
penaltyIdentity.captureFromMain(bundleId: self.currentBundleId)
367+
return probe(probeDeadline)
363368
}
364369
} catch {
365370
NSLog(

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCaptureTarget.swift‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,50 @@ enum SnapshotCommandPreparation {
2121
case capture(SnapshotCaptureTarget, systemSurface: SystemSurfaceHost?)
2222
}
2323

24+
/// The target a bounded XCTest probe arms its abandonment penalty with.
25+
///
26+
/// The hook that arms the penalty fires on the command queue the moment the probe's slice is spent,
27+
/// while the probe's own work block may still be running on main. `currentBundleId` belongs to main,
28+
/// so it is never read across that boundary: a caller that already took the identity on main hands it
29+
/// over, and a caller that is on the command queue lets the probe's main-side block capture the
30+
/// identity main holds once the work actually starts (#2781).
31+
enum SnapshotProbePenaltyTarget: Equatable {
32+
/// Identity a capture took on main when it prepared its target.
33+
case prepared(bundleId: String?)
34+
/// Read `currentBundleId` inside the probe's main-side block.
35+
case mainOwnedTarget
36+
}
37+
38+
/// One probe's penalty identity: written by the probe's main-side block, read by the command queue's
39+
/// abandonment hook through this lock.
40+
final class SnapshotProbePenaltyIdentity {
41+
private let lock = NSLock()
42+
private var bundleId: String?
43+
private let readsMainOwnedTarget: Bool
44+
45+
init(_ target: SnapshotProbePenaltyTarget) {
46+
readsMainOwnedTarget = target == .mainOwnedTarget
47+
if case .prepared(let bundleId) = target {
48+
self.bundleId = bundleId
49+
}
50+
}
51+
52+
/// Called on the main thread inside the probe's work block, before it enumerates anything, so the
53+
/// identity is the one main had settled on rather than one a queued write is about to replace.
54+
func captureFromMain(bundleId: String?) {
55+
guard readsMainOwnedTarget else { return }
56+
lock.lock()
57+
self.bundleId = bundleId
58+
lock.unlock()
59+
}
60+
61+
var penalizedBundleId: String? {
62+
lock.lock()
63+
defer { lock.unlock() }
64+
return bundleId
65+
}
66+
}
67+
2468
extension RunnerTests {
2569
/// Main thread only: reads the lifecycle-owned target identity.
2670
func takeSnapshotCaptureTarget(app: XCUIApplication) -> SnapshotCaptureTarget {

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+SnapshotCaptureTargetTests.swift‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ extension RunnerTests {
6161
}
6262
#endif
6363

64+
func testProbePenaltyIdentityTakesTheIdentityItsCallerMayLegallyKnow() {
65+
let prepared = SnapshotProbePenaltyIdentity(.prepared(bundleId: "com.example.prepared"))
66+
prepared.captureFromMain(bundleId: "com.example.settled")
67+
XCTAssertEqual(
68+
prepared.penalizedBundleId,
69+
"com.example.prepared",
70+
"a capture penalizes the target it was prepared for, whatever lifecycle binds meanwhile"
71+
)
72+
73+
let mainOwned = SnapshotProbePenaltyIdentity(.mainOwnedTarget)
74+
XCTAssertNil(
75+
mainOwned.penalizedBundleId,
76+
"a command-queue caller knows no identity until the probe's main-side block runs"
77+
)
78+
mainOwned.captureFromMain(bundleId: "com.example.settled")
79+
XCTAssertEqual(mainOwned.penalizedBundleId, "com.example.settled")
80+
}
81+
6482
func testMainOwnedSnapshotStateWriteRunsOnMainBeforeReturningWhenMainIsFree() {
6583
final class ResultBox {
6684
var ranOnMainThread: Bool?

0 commit comments

Comments
 (0)