Repository navigation
Hold a video export cancel until the session is exporting - #26338
Merged
crazytonyli merged 2 commits intoNov 5, 2026
Merged
Conversation
`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`.
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
statesloop has seen.exporting.MediaVideoExporterCancellationTests, which crashes the test host on Use export session states to observe video progress #26090 and passes here.Root Cause
Both problems happen in the first microseconds of an export:
states(updateInterval:)traps (EXC_BREAKPOINTinStateInternal.init(forSnapshotOf:previousInternalState:)) if the session leaves.exportingwhile the iterator builds its first.exportingvalue. A cancel at that moment does exactly that.export(to:as:)stuck forever, so neitheronCompletionnoronErrorruns.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.withTaskCancellationHandleronly records the request.ExportCancelGatecancels the export task once a cancel was requested and thestatesloop has seen.exporting. Cancelling from the loop body can't race the iterator's snapshot.export(to:as:),states(updateInterval:)and task cancellation.cancelExport(),progressandstatusare all deprecated from iOS 27 (statusfrom iOS 18) and aren't used.Task.checkCancellation()still reports the cancellation.What We Explored
Measured with the real
MediaVideoExporterin the WordPress test host on an iOS 27.0 simulator.states, cancel at once)session.progress, cancel at once.exportingstates, cancel held until.exporting(this PR)session.statusor the deprecatedprogress.statusalready warns at the iOS 18 minimum.Test plan
MediaVideoExporterCancellationTests(300 cancels aimed at export start): passes 3 of 3 runs at f314d83 and 3 of 3 at de63c5b. Without theMediaVideoExporter.swiftchange it crashes 2 of 2 runs with the trap above.cancelExport().swift formatandmake linton both files; no new warnings.