Skip to content

Stop reporting video export progress once the observer is stopped - #26082

Closed
jkmassel wants to merge 6 commits into
task/zero-warnings-avasset-asyncfrom
jkmassel/video-progress-observer-stop
Closed

jkmassel wants to merge 6 commits into
task/zero-warnings-avasset-asyncfrom
jkmassel/video-progress-observer-stop

Conversation

@jkmassel

@jkmassel jkmassel commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a race where a failed or cancelled video export ends with an unfinished Progress, despite fail() marking it as finished. Stacked on #26041.

Root Cause

VideoSessionProgressObserver.work() calls progressHandler before checking interrupt. observer.stop() (the defer in exportVideo(with:filename:progress:)) therefore doesn't prevent a tick that's already queued on the global queue — it still runs up to 100ms later.

When the export throws, the order is:

  1. defer { observer.stop() } → interrupt = true
  2. report catches the error → fail() sets completedUnitCount = totalUnitCount
  3. The queued tick runs → progressHandler(session.progress) → completedUnitCount = done * session.progress (e.g. 40)

Steps 1 and 2 are microseconds apart, so step 3 landing last is the likely ordering, not an edge case. The progress — and the parent progress in ItemProviderMediaExporter / MediaURLExporter — ends unfinished.

This isn't a regression from #26041: the old callback-based code also left the progress partial when the export failed. It does mean the "finished on failure" guarantee #26041 adds with fail() doesn't hold once the export has started.

Fix

  • WordPress/Classes/Utility/Media/MediaVideoExporter.swift: check interrupt before reporting, not after. interrupt is still an unsynchronized Bool (pre-existing), but with the check first a stale read only costs one extra tick instead of clobbering the final value.
  • Adds an init(sessionProgress:progressHandler:) seam so the observer can be tested without a real AVAssetExportSession, and moves the timer body into tick() so tests can drive it without waiting on the timer. init(videoSession:progressHandler:) is kept as a convenience initializer, so the call site is unchanged. The class goes from fileprivate to internal for @testable access.

Test plan

Real video-export tests in this bundle are skipped as flaky (MediaURLExporterTests.exportTestVideo), so the new tests drive the observer directly: a stubbed progress source, and tick() called directly — no sleeps, no AVFoundation, no scheduling dependence.

  • VideoSessionProgressObserverTests.doesNotReportAfterStop — stops the observer, marks the progress finished (what fail() does), then runs a tick; expects it to stay finished. Fails without the fix (completedUnitCount → 40 vs totalUnitCount → 100), passes with it.
  • VideoSessionProgressObserverTests.reportsProgress — guards against the fix silencing the observer entirely.

crazytonyli and others added 5 commits September 24, 2026 15:18
The success handler already ran on the main thread, but the failure handler ran on the exporter's background thread.
Export videos with the async export API, which cancels with the task. If the duration can't be loaded, the export continues, as it did with the synchronous duration property.
`VideoSessionProgressObserver.work()` called the progress handler before
checking `interrupt`, so a tick already queued when the export ended ran
after `fail()` marked the progress as finished and overwrote it with the
session's partial progress.

Check `interrupt` first, and inject the progress read so the observer can
be tested without a real `AVAssetExportSession`.
@jkmassel jkmassel added this to the 27.4 milestone Sep 24, 2026
@jkmassel jkmassel added the Media label Sep 24, 2026
@jkmassel jkmassel self-assigned this Sep 24, 2026
@wpmobilebot

wpmobilebot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number34680
VersionPR #26082
Bundle IDorg.wordpress.alpha
Commit40f879c
Installation URL6719rlg5t6pmg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number34680
VersionPR #26082
Bundle IDcom.jetpack.alpha
Commit40f879c
Installation URL19bd6vurvjq7g
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@crazytonyli

Copy link
Copy Markdown
Contributor

I noticed the issue with this observe and left it alone, because I plan to update the minimal iOS version to 18 (which we bump every year), and use this API instead.

Move the timer body into `tick()` so the tests can call it directly.
The previous tests slept 300ms against a 100ms timer, which could fail
on a slow CI agent.
@crazytonyli

Copy link
Copy Markdown
Contributor

@jkmassel Does your thumbup on this comment mean we can close this PR, and do something like this instead: #26090?

@crazytonyli
crazytonyli force-pushed the task/zero-warnings-avasset-async branch from eee87cc to 6606ea3 Compare October 19, 2026 00:21
@wpmobilebot wpmobilebot modified the milestones: 27.4, 27.5 Oct 20, 2026
@wpmobilebot

Copy link
Copy Markdown
Contributor

Version 27.4 has now entered code-freeze, so the milestone of this PR has been updated to 27.5.

@jkmassel

Copy link
Copy Markdown
Contributor Author

Yes, close this one in favour of #26090. The task group waits for the progress task before fail() runs, so the stale tick can't happen, and the observer goes away. Until #26154 lands, a failed or cancelled export can still end with unfinished progress. We saw no user-visible effect, and trunk ends unfinished too.

@jkmassel jkmassel closed this Oct 31, 2026
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.

3 participants