photos: tear down video export state on cancel, failure, and start error
- Commit
ee29ecf5a95c516fd76dc1964dd3eaba548997b4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:17 UTC (28 days ago)
- Parents
9f8607f
Video export teardown left the in-flight temp file and the exporting flag behind on three paths. First, cancelExport deleted only the overwrite-permission pending file: Media3 Transformer.cancel fires no listener callback, so the listener's own temp-file cleanup never ran and every mid-export cancel leaked a partial video_edit_*.mp4 into cacheDir. The view model now tracks the in-flight output and deletes it on cancel, and likewise on ViewModel clear. Second, a synchronous Transformer.start throw (e.g. the source URI vanished between load and export) skipped every callback, leaving exporting=true forever behind a progress dialog that could never resolve; start is now guarded and tears down plus fires onComplete per its documented contract. Third, the MediaStore-copy error branch never deleted the temp file it could no longer consume. Cancel during the final MediaStore copy still lets the copy finish: ownership of the file has already transferred by then, and the window is a fast local copy. Verified with a new exportStartFailureResetsStateAndLeavesNoTempFile regression test (unknown-URI export completes the callback, resets exporting, and leaves no temp orphans); targeted :photos:testDevUnitTest run green.
Diff
photos/src/main/java/com/vayunmathur/photos/util/VideoEditViewModel.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/VideoEditViewModel.kt b/photos/src/main/java/com/vayunmathur/photos/util/VideoEditViewModel.ktindex 6a453701aa..1a113666cb 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/VideoEditViewModel.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/VideoEditViewModel.kt@@ -88,6 +88,11 @@ class VideoEditViewModel( private var pendingExportUri: Uri? = null private var pendingOnComplete: (() -> Unit)? = null + // The transformer's in-flight output. Tracked so cancel/clear can delete+ // it: Transformer.cancel() fires no listener callback, so the listener's+ // own tempFile.delete() never runs on that path.+ private var activeExportFile: File? = null+ /** Loads [id]'s video from the DB, falling back to a stand-in built from [initialUri]. */ fun loadVideo(id: Long, initialUri: String?) { photoJob?.cancel()@@ -209,6 +214,7 @@ class VideoEditViewModel( if (_exporting.value) return val state = _state.value val tempFile = File(context.cacheDir, "video_edit_${System.currentTimeMillis()}.mp4")+ activeExportFile = tempFile val mediaItem = buildMediaItem(photo.uri.toUri(), state) val editedItem = EditedMediaItem.Builder(mediaItem) .setRemoveAudio(state.muted)@@ -219,6 +225,7 @@ class VideoEditViewModel( .addListener(object : Transformer.Listener { override fun onCompleted(composition: Composition, exportResult: ExportResult) { stopProgress()+ activeExportFile = null finishExport(context, photo, asCopy, tempFile, onComplete) } @@ -230,6 +237,7 @@ class VideoEditViewModel( Log.error(TAG, "Export failed", exportException) stopProgress() tempFile.delete()+ activeExportFile = null _exporting.value = false onComplete() }@@ -238,7 +246,20 @@ class VideoEditViewModel( transformer = t _exporting.value = true _progress.value = 0f- t.start(editedItem, tempFile.absolutePath)+ try {+ t.start(editedItem, tempFile.absolutePath)+ } catch (e: Exception) {+ // Synchronous start failure (e.g. the source URI vanished): no+ // listener callback will ever arrive, so tear down here or the+ // UI sits on a progress dialog that never resolves.+ Log.error(TAG, "Export start failed", e)+ tempFile.delete()+ activeExportFile = null+ transformer = null+ _exporting.value = false+ onComplete()+ return+ } startProgress() } @@ -265,6 +286,8 @@ class VideoEditViewModel( fun cancelExport() { transformer?.cancel() stopProgress()+ activeExportFile?.delete()+ activeExportFile = null pendingExportFile?.delete() pendingExportFile = null pendingExportUri = null@@ -296,6 +319,7 @@ class VideoEditViewModel( } is WriteResult.Error -> { Log.error(TAG, "Save failed", result.exception)+ tempFile.delete() _exporting.value = false onComplete() }@@ -346,6 +370,7 @@ class VideoEditViewModel( progressJob?.cancel() transformer?.cancel() transformer = null+ activeExportFile?.delete() pendingExportFile?.delete() }
photos/src/test/java/com/vayunmathur/photos/photoslice2/Slice2Pass2LogicTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/Slice2Pass2LogicTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/Slice2Pass2LogicTest.ktindex 2b40e96c9f..ad6fb9c915 100644--- a/photos/src/test/java/com/vayunmathur/photos/photoslice2/Slice2Pass2LogicTest.kt+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/Slice2Pass2LogicTest.kt@@ -35,6 +35,7 @@ import com.vayunmathur.photos.ui.EditorMode import com.vayunmathur.photos.ui.LayersPanel import com.vayunmathur.photos.util.LayerCompositor import com.vayunmathur.photos.util.VideoEditViewModel+import com.vayunmathur.photos.util.pass3Photo import org.junit.Rule import org.junit.runner.RunWith import org.robolectric.Robolectric@@ -198,6 +199,20 @@ class Slice2Pass2LogicTest { vm.cancelExport() } + @Test fun exportStartFailureResetsStateAndLeavesNoTempFile() {+ val vm = VideoEditViewModel(app, PhotosRepository.get(app))+ var completed = false+ // Unknown content URI: Transformer.start() throws synchronously.+ vm.export(app, pass3Photo(1, "clip.mp4"), true) { completed = true }+ assertTrue(completed, "onComplete must fire on unrecoverable failure")+ assertFalse(vm.exporting.value)+ val orphans = app.cacheDir.listFiles { f -> f.name.startsWith("video_edit_") } ?: emptyArray()+ assertTrue(orphans.isEmpty(), "failed export must leave no temp file")+ // Cancel stays a safe no-op after a failed start.+ vm.cancelExport()+ assertFalse(vm.exporting.value)+ }+ // ---- Overlays / canvas / layers panel ---- @Test fun toolOverlaysAcrossModes() {