Skip to content

Hold a video export cancel until the session is exporting - #26338

Merged
crazytonyli merged 2 commits into
task/ios-18-export-progressfrom
jkmassel/defer-video-export-cancel
Nov 5, 2026
Merged

crazytonyli merged 2 commits into
task/ios-18-export-progressfrom
jkmassel/defer-video-export-cancel

Conversation

@jkmassel

@jkmassel jkmassel commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

From Claude: this keeps states(updateInterval:) from #26090 and fixes the crash it causes when a video export is cancelled just as it starts. It also fixes a pre-existing lost callback from #26041.

Summary

Root Cause

Both problems happen in the first microseconds of an export:

  • Crash on Use export session states to observe video progress #26090: states(updateInterval:) traps (EXC_BREAKPOINT in StateInternal.init(forSnapshotOf:previousInternalState:)) if the session leaves .exporting while the iterator builds its first .exporting value. A cancel at that moment does exactly that.
  • Lost callback, already on trunk: a cancel at the same moment can leave export(to:as:) stuck forever, so neither onCompletion nor onError runs.

Both come from the same thing: task cancellation makes export(to:as:) cancel the session immediately, whatever state the session is in.

Fix

  • export(to:as:) runs in an unstructured task, so cancelling the caller doesn't cancel the session.
  • withTaskCancellationHandler only records the request.
  • ExportCancelGate cancels the export task once a cancel was requested and the states loop has seen .exporting. Cancelling from the loop body can't race the iterator's snapshot.
  • The fix uses only export(to:as:), states(updateInterval:) and task cancellation. cancelExport(), progress and status are all deprecated from iOS 27 (status from iOS 18) and aren't used.
  • If the export finishes before the cancel is sent, Task.checkCancellation() still reports the cancellation.

What We Explored

Measured with the real MediaVideoExporter in the WordPress test host on an iOS 27.0 simulator.

Version Cancel as export starts (900) Cancel 0–300 ms in (600) Random cancel (4,000) Cancel → callback, median
#26090 (states, cancel at once) crashed 3 of 3 runs 0 crashes not run —
Poll session.progress, cancel at once 0 crashes 0 crashes 6 lost callbacks ~10 ms
Poll, cancel held until .exporting 0 crashes 0 crashes 0 lost ~140–165 ms ❌
states, cancel held until .exporting (this PR) 0 crashes 0 crashes 0 lost ~15–20 ms ✅
  • The polling variants also need session.status or the deprecated progress. status already warns at the iOS 18 minimum.
  • With no cancel, all four variants completed 20 of 20 exports and reported progress partway through.
  • The PR's own code never crashed on a cancel 0–300 ms into the export, so the trap is limited to the start.

Test plan

  • MediaVideoExporterCancellationTests (300 cancels aimed at export start): passes 3 of 3 runs at f314d83 and 3 of 3 at de63c5b. Without the MediaVideoExporter.swift change it crashes 2 of 2 runs with the trap above.
  • The 5,500-cancel measurements above, run against a prototype with the same code behind a test-only switch. That prototype called cancelExport().
  • Re-run after switching to task cancellation (de63c5b): 0 crashes in 900 cancels aimed at the start, 600 mid-export and 4,000 random. 0 lost callbacks. Median ~17–20 ms from cancel to callback. 20 of 20 exports without a cancel completed with progress partway through.
  • swift format and make lint on both files; no new warnings.
  • iOS 18 and 26: not done. The test host doesn't launch on them with Xcode 27. The same trap was seen on both in standalone test programs during the Use export session states to observe video progress #26090 review.
  • Physical device and the full app UI: not done.

`states(updateInterval:)` traps if the session leaves `.exporting` while
the iterator builds its first `.exporting` value, so a cancel at the
start of an export crashed the app. A cancel at the same moment could
also leave the export stuck with no callback.

`export(to:as:)` now runs in an unstructured task, so cancelling the
caller doesn't cancel the session straight away. `ExportCancelGate`
sends `cancelExport()` once the cancel is requested and the `states`
loop has seen `.exporting`.
@jkmassel jkmassel added the Media label Nov 4, 2026
@jkmassel jkmassel self-assigned this Nov 4, 2026
@jkmassel
jkmassel requested a review from crazytonyli November 4, 2026 22:34
`cancelExport()` is deprecated from iOS 27 in favour of task cancellation.
Cancelling the task that runs `export(to:as:)` cancels the session, and
the gate still decides when that happens.
@crazytonyli
crazytonyli merged commit ceb15c0 into task/ios-18-export-progress Nov 5, 2026
22 checks passed
@crazytonyli
crazytonyli deleted the jkmassel/defer-video-export-cancel branch November 5, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants