Skip to content

Commit 04fb465

Browse files
committed
fix: don't re-enter main for bookkeeping behind an abandoned snapshot probe (#1244)
After the bounded system-modal probe times out, runMainThreadWork abandons its query but that query keeps grinding on the main thread. The capture plan recovers through the independent private-AX backend and returns — but executeSnapshotDispatched then ran its didRecordXCTestFailure/retry bookkeeping through another runMainThreadWork hop, which queues behind the same abandoned query and re-stalls the command for up to the 30s execution watchdog (or throws), reintroducing the very stall the recovery avoided. Skip that bookkeeping while abandoned XCTest work is outstanding — the policy setNeedsPostSnapshotInteractionDelay already uses — so the recovered response returns immediately and a subsequent command still reports RUNNER_BUSY until the work drains. Adds an in-bundle regression that fails if the guard is removed. The snapshot recovery loop is factored into executeDispatchedWithRecovery so the guard is exercisable without a live capture. The alert command's SpringBoard detection is intentionally left on its existing 30s command watchdog here; giving alert its own physical-iOS capability is tracked as a focused follow-up (#1231).
1 parent d216670 commit 04fb465

2 files changed

Lines changed: 75 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,18 @@ extension RunnerTests {
836836
}
837837

838838
private func executeSnapshotDispatched(command: Command) throws -> Response {
839+
try executeDispatchedWithRecovery(command: command) {
840+
try self.executeSnapshotDispatchedOnce(command: command)
841+
}
842+
}
843+
844+
/// The dispatched snapshot recovery loop: read-only retry + XCTest-recorded-failure invalidation,
845+
/// matching what `executeOnMainSafely` gives the generic path. `perform` runs the capture and its
846+
/// own bounded main-thread work.
847+
func executeDispatchedWithRecovery(
848+
command: Command,
849+
perform: () throws -> Response
850+
) throws -> Response {
839851
var hasRetried = false
840852
while true {
841853
let failureCountBefore = try runMainThreadWork(
@@ -845,7 +857,16 @@ extension RunnerTests {
845857
) {
846858
self.currentXCTestFailureCount()
847859
}
848-
let response = try executeSnapshotDispatchedOnce(command: command)
860+
let response = try perform()
861+
// Recovered independently — re-entering main for bookkeeping would queue behind the still-
862+
// abandoned XCTest query and re-stall the command (#1244), so skip it until that work drains.
863+
if hasAbandonedTreeCapture() {
864+
NSLog(
865+
"AGENT_DEVICE_RUNNER_DISPATCH_RECOVERY_SKIPPED_XCTEST_OCCUPIED command=%@",
866+
command.command.rawValue
867+
)
868+
return response
869+
}
849870
let recordedFailureResponse = try runMainThreadWork(
850871
command: command,
851872
timeout: mainThreadExecutionTimeout,

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

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,13 @@ extension RunnerTests {
281281
return try runMainThreadWork(
282282
command: nil,
283283
timeout: slice,
284-
timeoutError: systemModalProbeTimeoutError(slice: slice),
284+
timeoutError: {
285+
SnapshotCaptureFailure(
286+
code: Self.xCTestSnapshotTimeoutCode,
287+
message: "the system-modal probe exceeded its \(slice)s time slice",
288+
hint: "The capture plan recovers through non-XCTest snapshot tiers while the modal probe drains."
289+
)
290+
},
285291
onAbandoned: {
286292
self.retainAbandonedXCTestChannelWork()
287293
NSLog("AGENT_DEVICE_RUNNER_SYSTEM_MODAL_PROBE_TIMEOUT slice=%.1f", slice)
@@ -318,16 +324,6 @@ extension RunnerTests {
318324
return min(budget, deadlineRemaining)
319325
}
320326

321-
private func systemModalProbeTimeoutError(slice: TimeInterval) -> () -> Error {
322-
{
323-
SnapshotCaptureFailure(
324-
code: Self.xCTestSnapshotTimeoutCode,
325-
message: "the system-modal probe exceeded its \(slice)s time slice",
326-
hint: "The capture plan recovers through non-XCTest snapshot tiers while the modal probe drains."
327-
)
328-
}
329-
}
330-
331327
func rawTreeSnapshotPayload(
332328
context: SnapshotTraversalContext,
333329
options: SnapshotOptions
@@ -558,6 +554,52 @@ extension RunnerTests {
558554
XCTAssertEqual(Self.systemModalProbeSlice(budget: 4, deadlineRemaining: 0), 0)
559555
XCTAssertEqual(Self.systemModalProbeSlice(budget: 4, deadlineRemaining: -5), 0)
560556
}
557+
558+
func testDispatchRecoverySkipsBookkeepingWhileXCTestChannelOccupied() {
559+
// The #1244 recovery shape: the modal probe abandoned an XCTest query that is still grinding on
560+
// main, the capture recovered independently, and its response is ready. The recovery loop must
561+
// return it without re-entering the main queue for recorded-failure/retry bookkeeping (that hop
562+
// would block behind the abandoned query and re-stall the command), and a later command must
563+
// still see the runner busy until the abandoned work drains. Removing the guard regresses this.
564+
let command = try! JSONDecoder().decode(
565+
Command.self,
566+
from: Data(#"{"command":"snapshot","commandId":"recovery-guard"}"#.utf8)
567+
)
568+
let recovered = Response(ok: false, error: ErrorPayload(message: "target is not available"))
569+
570+
setAbandonedXCTestWork(1)
571+
defer { setAbandonedXCTestWork(0) }
572+
guard case .busy = currentMainThreadBusyState() else {
573+
return XCTFail("expected RUNNER_BUSY while abandoned XCTest work is outstanding")
574+
}
575+
576+
var occupiedCalls = 0
577+
let occupied = try! executeDispatchedWithRecovery(command: command) {
578+
occupiedCalls += 1
579+
return recovered
580+
}
581+
XCTAssertEqual(occupiedCalls, 1, "recovered response must not retry behind abandoned XCTest work")
582+
XCTAssertEqual(occupied.ok, false)
583+
584+
setAbandonedXCTestWork(0)
585+
guard case .idle = currentMainThreadBusyState() else {
586+
return XCTFail("runner should be idle once the abandoned work drained")
587+
}
588+
var drainedCalls = 0
589+
_ = try! executeDispatchedWithRecovery(command: command) {
590+
drainedCalls += 1
591+
return recovered
592+
}
593+
XCTAssertEqual(drainedCalls, 2, "with the channel free the read-only retry runs once")
594+
}
595+
596+
private func setAbandonedXCTestWork(_ count: Int) {
597+
treeCaptureLock.lock(); abandonedTreeCaptureCount = count; treeCaptureLock.unlock()
598+
mainThreadWorkLock.lock()
599+
abandonedMainThreadWorkCount = count
600+
abandonedMainThreadWorkSince = count > 0 ? Date(timeIntervalSinceNow: -1) : nil
601+
mainThreadWorkLock.unlock()
602+
}
561603
#endif
562604

563605
private func interactiveRootNode(rect: CGRect) -> SnapshotNode {

0 commit comments

Comments
 (0)