Give every job a staging path of its own

`<cacheDir>/conversions/` is shared by the convert tab, the join tab and `ConcatEngine`, and
until now none of the three named a file that belonged to one job. A conversion derived its name
from the input's display name, so two `holiday.mp4` from different folders wrote the same file.
A join used the constant `joined.<ext>`, so any two joins of one format did. The list file was
the constant `concat_list.txt`, so any two joins at all did, and one of them would read the
other's input list.

The naming half is not a hypothesis. Two independent conversions on a Pixel each computed
`cache/conversions/input_converted.mp4`, the second silently overwrote the first, and a tag query
in a fresh process then returned **two SUCCEEDED `WorkInfo`s naming that one file** with one file
on disk. That is the collision reaching the point where it makes a *fix* ambiguous rather than
just a file: `Reattachment` can offer the bytes, because they are the user's either way, but it
cannot say which job produced them.

`StagingNames` keys the name on the WorkManager request id. That id is what stays still across a
retry -- `WorkerWrapper` builds `WorkerParameters` from the `WorkSpec` id and only increments
`runAttemptCount` -- which matters more here than uniqueness does, and matters more since the
previous commit made retries routine. A failed attempt deletes its staged file on the way out,
and that only collects the partial the *previous* attempt left when the name has not moved.

Opaque rather than sanitised, deliberately. The staged name is never shown to anyone: `save()`
recomputes a suggested name and the user picks the real one in the SAF dialog. So there was
nothing to lose by dropping the display name, and something to gain -- a provider-supplied
display name can contain a separator, be empty, or be four kilobytes long, and `File(stagingDir,
"../escape_converted.mp4")` resolves to a path outside staging. That was reachable before this
commit and is now unreachable by construction rather than by a sanitiser that has to be right
about every case. There is a test for exactly that name.

The extension stays, and is not decoration: `FFmpegConcatCommand` names no output muxer, so
FFmpeg infers it from the output path. A fully opaque name would quietly produce the wrong
container.

`ConcatEngine`'s list file is derived from the output it belongs to rather than taking another
parameter, so the two cannot drift apart, a directory listing shows which list belongs to which
join, and the sweep ages them together.

Three neighbouring comments claimed things that are no longer true, and are corrected rather than
left to mislead the next reader:

  - `Reattachment.Ambiguous` said it "resolves on its own once each job stages under a name of
    its own". It now does -- for work enqueued from here on. The case is **kept**, because the
    queue outlives the change: WorkManager holds finished work for about a week, and the jobs
    likeliest to be sitting in it when this code first runs are the ones named the old way.
    Behaviour is unchanged and `ReattachmentTest` is untouched.
  - `OutputPublisher.sweepStaging` justified its age rule partly on there being "no per-job
    namespacing". There is now, and the rule still stands on its own: per-job names stop two jobs
    from sharing a file, and say nothing about whether a file's job is still running, which is the
    question a sweep actually asks. Same for `StagingSweep` and the note in
    `LibreMediaConverterApp`.
  - Both ViewModels' `reattach()` explained aliasing as something nothing prevented. Narrowed to
    what is still true of work already in the queue.

`ConcatEngineTest` asks `StagingNames` for the list file's name instead of spelling out
`concat_list.txt`. That is the difference between a test and a tautology: a literal there would
have gone on passing after the rename while asserting that a file nothing creates does not exist.
The same trap was live in the two worker tests from the previous commits, whose staged-file
assertions computed a path of their own -- they now assert on the staging directory being empty,
which cannot go vacuous when a name moves.

`PerJobStagingTest` drives the real worker, because the naming function was never the part that
was wrong: what was wrong was which name the worker asked for. Two jobs converting one file must
leave two files; a second attempt at one job must not leave a second; and a display name that
climbs out of staging must not. The first and third fail before the change with "each job must
have staged its own file, found [input_converted.mp4] expected:<2> but was:<1>" and "the output
belongs in staging expected:<1> but was:<0>" -- the latter because the file had landed in
`cacheDir` instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-22 20:11:44 -05:00
co-authored by Claude Opus 5
parent dcdcbfd3af
commit 2a68f03134
16 changed files with 385 additions and 44 deletions
@@ -8,6 +8,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import java.io.File
import java.util.UUID
/**
* What a pure function cannot say: the file is really gone.
@@ -72,9 +73,15 @@ class OutputPublisherStagingTest {
@Test
fun `the sweep collects an orphan and leaves a live job alone`() {
val orphan = publisher.createStagingFile("orphan.mp4").apply { writeBytes(ByteArray(4096)) }
val liveOutput = publisher.createStagingFile("joined.mp4").apply { writeBytes(ByteArray(4096)) }
val liveList = publisher.createStagingFile("concat_list.txt").apply { writeText("file 'a.mp4'\n") }
// Named the way the app names them, so the sweep is exercised against real shapes.
val liveJob = StagingNames.forJob(UUID.randomUUID(), "mp4")
val orphan = publisher.createStagingFile(
StagingNames.forJob(UUID.randomUUID(), "mp4"),
).apply { writeBytes(ByteArray(4096)) }
val liveOutput = publisher.createStagingFile(liveJob).apply { writeBytes(ByteArray(4096)) }
val liveList = publisher.createStagingFile(
StagingNames.concatListFor(liveJob),
).apply { writeText("file 'a.mp4'\n") }
assertTrue(orphan.setLastModified(System.currentTimeMillis() - StagingSweep.GRACE_PERIOD_MS - 60_000))
publisher.sweepStaging()
@@ -0,0 +1,89 @@
package org.libremediaconverter.convert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.libremediaconverter.model.OutputFormat
import java.io.File
import java.util.UUID
/**
* The rule that gives every job a staging path of its own.
*
* A pure function for the same reason as [StagingSweep] and
* [org.libremediaconverter.work.FailureOutcome]: what it prevents cannot be provoked here. The
* collision needs two jobs alive at once, one of them resumed by WorkManager after a process
* restart, which is a device and an `am kill`. What *is* checkable is the property that makes the
* collision impossible, and that is what these pin.
*/
class StagingNamesTest {
@Test
fun `two jobs converting the same file stage under different names`() {
// The collision, seen on a device: two independent jobs both computed
// cache/conversions/input_converted.mp4, and a tag query in a fresh process returned two
// SUCCEEDED WorkInfos naming that one file.
assertNotEquals(
StagingNames.forJob(JOB_A, MP4.extension),
StagingNames.forJob(JOB_B, MP4.extension),
)
}
@Test
fun `the same job stages under the same name on every attempt`() {
// Load-bearing, not incidental. A retry runs doWork() from the top, and the catch on the
// way out deletes the staged file -- which only collects the previous attempt's partial if
// the name is the same. WorkManager builds WorkerParameters from the WorkSpec id and only
// increments runAttemptCount, so the id is what stays still across a retry.
assertEquals(
StagingNames.forJob(JOB_A, MP4.extension),
StagingNames.forJob(JOB_A, MP4.extension),
)
}
@Test
fun `the extension is the output's, because that is what infers the muxer`() {
// Not cosmetic. FFmpegConcatCommand names no output muxer, so FFmpeg infers it from the
// path -- an opaque name without the right extension would silently produce the wrong
// container.
assertTrue(StagingNames.forJob(JOB_A, MP4.extension).endsWith(".mp4"))
assertTrue(StagingNames.forJob(JOB_A, OutputFormat.MKV_H265.extension).endsWith(".mkv"))
assertTrue(StagingNames.forJob(JOB_A, OutputFormat.M4A_AAC.extension).endsWith(".m4a"))
}
@Test
fun `a staging name is a bare filename and nothing else`() {
// The alternative to an opaque name was sanitising the provider-supplied display name,
// which can contain a separator, be empty, or be four kilobytes long. This is what makes
// that whole question moot.
val name = StagingNames.forJob(JOB_A, MP4.extension)
assertEquals("a staging name must not be a path", name, File(name).name)
assertTrue("a staging name must not be empty", name.isNotEmpty())
}
@Test
fun `each join gets a list file of its own`() {
// ConcatEngine used a constant, so any two joins at once shared one concat_list.txt and
// one of them read the other's input list.
assertNotEquals(
StagingNames.concatListFor(StagingNames.forJob(JOB_A, MP4.extension)),
StagingNames.concatListFor(StagingNames.forJob(JOB_B, MP4.extension)),
)
}
@Test
fun `a list file is named after the output it belongs to`() {
// So the pair is obvious in a directory listing, and so the sweep ages them together.
val output = StagingNames.forJob(JOB_A, MP4.extension)
val list = StagingNames.concatListFor(output)
assertEquals("$JOB_A.concat_list.txt", list)
assertTrue(list.startsWith(output.substringBeforeLast('.')))
}
private companion object {
val MP4 = OutputFormat.MP4_H264
val JOB_A: UUID = UUID.fromString("00000000-0000-4000-8000-00000000000a")
val JOB_B: UUID = UUID.fromString("00000000-0000-4000-8000-00000000000b")
}
}
@@ -15,12 +15,12 @@ import com.google.common.util.concurrent.ListenableFuture
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.libremediaconverter.convert.ConversionDependencies
import org.libremediaconverter.convert.OutputPublisher
import org.libremediaconverter.convert.StagingNames
import org.libremediaconverter.convert.installTestWorkManager
import org.libremediaconverter.model.OutputFormat
import org.robolectric.RobolectricTestRunner
@@ -92,12 +92,20 @@ class DeniedForegroundStartTest {
fun `a denied foreground start collects the partial file the killed attempt left behind`() {
// Exactly the 2 MB orphan the device pass found. A process killed mid-transcode leaves a
// partial in staging, and the attempt WorkManager schedules to recover it stages under the
// same name; reaching staged.delete() is what collects it.
val staged = stagedFile().apply { writeBytes(ByteArray(PARTIAL_BYTES)) }
// same name -- the job id does not move across a retry -- so reaching staged.delete() is
// what collects it.
stagedFile().writeBytes(ByteArray(PARTIAL_BYTES))
runBlocking { conversionWorker().doWork() }
assertFalse("a denied restart must not orphan the previous attempt's partial", staged.exists())
// Asserted against the whole directory rather than one path. A path this test computes
// itself can stop matching the one the worker computes, and then the assertion passes by
// asking whether a file nobody wrote is absent.
assertEquals(
"a denied restart must not orphan the previous attempt's partial",
emptyList<String>(),
stagedNames(),
)
}
@Test
@@ -146,7 +154,9 @@ class DeniedForegroundStartTest {
.build()
/** The staging path the worker will compute, asked for rather than spelled out here. */
private fun stagedFile(): File = publisher.createStagingFile(ConversionWorker.outputNameFor(DISPLAY_NAME, SPEC))
private fun stagedFile(): File = publisher.createStagingFile(StagingNames.forJob(CONVERSION_ID, SPEC.extension))
private fun stagedNames(): List<String> = stagedFile().parentFile?.listFiles().orEmpty().map { it.name }.sorted()
private companion object {
val INPUT: Uri = Uri.parse("content://test/holiday.mp4")
@@ -0,0 +1,152 @@
package org.libremediaconverter.work
import android.app.Application
import android.net.Uri
import androidx.media3.common.util.UnstableApi
import androidx.work.Data
import androidx.work.testing.TestListenableWorkerBuilder
import androidx.work.workDataOf
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.libremediaconverter.convert.ConversionDependencies
import org.libremediaconverter.convert.OutputPublisher
import org.libremediaconverter.convert.SoftwareTranscoder
import org.libremediaconverter.convert.installTestWorkManager
import org.libremediaconverter.model.ConversionRequest
import org.libremediaconverter.model.DeviceCodecs
import org.libremediaconverter.model.EnginePreference
import org.libremediaconverter.model.InputProbe
import org.libremediaconverter.model.OutputFormat
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import java.io.File
import java.util.UUID
/**
* That two jobs cannot write the same staged file.
*
* Observed rather than imagined: two independent conversions on a Pixel each computed
* `cache/conversions/input_converted.mp4`, the second overwrote the first, and a tag query in a
* fresh process then returned two SUCCEEDED `WorkInfo`s naming that one file — which is what
* makes reattachment ambiguous about which job produced what is on disk.
*
* These drive the real worker rather than the naming function, because the naming function was
* never the part that was wrong. What was wrong is which name the worker asked for.
*/
@UnstableApi
@RunWith(RobolectricTestRunner::class)
class PerJobStagingTest {
private lateinit var app: Application
private lateinit var publisher: OutputPublisher
private lateinit var stagingDir: File
@Before
fun setUp() {
app = RuntimeEnvironment.getApplication()
publisher = AlwaysRoomPublisher(app)
ConversionDependencies.publisher = { publisher }
ConversionDependencies.probe = { _, _ -> InputProbe() }
ConversionDependencies.deviceCodecs = { DeviceCodecs.PERMISSIVE }
ConversionDependencies.software = { WritingTranscoder }
installTestWorkManager(app, Data.EMPTY)
stagingDir = publisher.createStagingFile("anything").parentFile!!
stagingDir.listFiles()?.forEach { it.delete() }
}
@After
fun tearDown() {
ConversionDependencies.reset()
}
@Test
fun `two conversions of the same file stage under names of their own`() {
runBlocking { conversionWorker(JOB_A).doWork() }
runBlocking { conversionWorker(JOB_B).doWork() }
// Two jobs, two files. One file here means the second job overwrote the first's output
// while both went on reporting that path as their result.
assertEquals(
"each job must have staged its own file, found ${stagedNames()}",
2,
stagedNames().size,
)
}
@Test
fun `a second attempt at one job reuses the first attempt's staging path`() {
runBlocking { conversionWorker(JOB_A, runAttemptCount = 0).doWork() }
val first = stagedNames()
runBlocking { conversionWorker(JOB_A, runAttemptCount = 1).doWork() }
// A per-attempt name would leak one file per retry, and would stop the catch on the way
// out of a failed attempt from collecting the partial the previous one left.
assertEquals("a retry must not stage under a new name", first, stagedNames())
}
@Test
fun `a display name that tries to climb out of staging cannot`() {
// Display names come from a document provider and are not this app's to trust: one can
// contain a separator, be empty, or be four kilobytes long. Deriving the staged path from
// it put all of that on a filesystem path. Naming the job instead retires the question
// rather than answering it with a sanitiser.
runBlocking { conversionWorker(JOB_A, displayName = "../escape.mp4").doWork() }
assertEquals("the output belongs in staging", 1, stagedNames().size)
assertFalse(
"nothing may be written outside the staging directory",
File(app.cacheDir, "escape_converted.mp4").exists(),
)
}
private fun stagedNames(): List<String> = stagingDir.listFiles().orEmpty().map { it.name }.sorted()
private fun conversionWorker(
id: UUID,
runAttemptCount: Int = 0,
displayName: String = DISPLAY_NAME,
): ConversionWorker = TestListenableWorkerBuilder<ConversionWorker>(
context = app,
inputData = workDataOf(
ConversionWorker.KEY_INPUT_URI to INPUT.toString(),
ConversionWorker.KEY_DISPLAY_NAME to displayName,
ConversionWorker.KEY_SIZE_BYTES to INPUT_BYTES,
ConversionWorker.KEY_CONTAINER to SPEC.container.name,
ConversionWorker.KEY_VIDEO_CODEC to SPEC.videoCodec.name,
ConversionWorker.KEY_AUDIO_CODEC to SPEC.audioCodec.name,
ConversionWorker.KEY_ENGINE_PREFERENCE to EnginePreference.FORCE_SOFTWARE.name,
),
runAttemptCount = runAttemptCount,
).setId(id).build()
private companion object {
val INPUT: Uri = Uri.parse("file:///tmp/input.mp4")
const val DISPLAY_NAME = "input.mp4"
const val INPUT_BYTES = 1024L
val SPEC = OutputFormat.MP4_H265.spec
val JOB_A: UUID = UUID.fromString("00000000-0000-4000-8000-00000000000a")
val JOB_B: UUID = UUID.fromString("00000000-0000-4000-8000-00000000000b")
}
}
/** An engine that only writes the file, which is the whole of what these tests look at. */
private object WritingTranscoder : SoftwareTranscoder {
override suspend fun run(
request: ConversionRequest,
inputPath: String,
output: File,
durationMs: Long,
onProgress: (Int) -> Unit,
) {
output.writeBytes(ByteArray(OUTPUT_BYTES))
}
private const val OUTPUT_BYTES = 512
}
@@ -11,7 +11,6 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@@ -86,9 +85,10 @@ class WorkerCancellationTest {
runCatching { runBlocking { worker.doWork() } }
// The engine stub writes before it throws, so this file really existed. Rethrowing without
// deleting would trade one defect for another.
assertFalse("a cancelled attempt must not leave its partial behind", stagedFile().exists())
// The engine stub writes before it throws, so a file really existed. Asserted against the
// whole directory rather than one path, so a name this test computes drifting from the
// worker's cannot turn it into a question about a file nobody wrote.
assertEquals("a cancelled attempt must not leave its partial behind", emptyList<String>(), stagedNames())
}
@Test
@@ -105,7 +105,7 @@ class WorkerCancellationTest {
),
result,
)
assertFalse("a failed attempt must not leave its partial behind", stagedFile().exists())
assertEquals("a failed attempt must not leave its partial behind", emptyList<String>(), stagedNames())
}
/**
@@ -134,8 +134,8 @@ class WorkerCancellationTest {
).setId(JOB_ID).build()
}
/** The staging path the worker will compute, asked for rather than spelled out here. */
private fun stagedFile(): File = publisher.createStagingFile(ConversionWorker.outputNameFor(DISPLAY_NAME, SPEC))
private fun stagedNames(): List<String> =
publisher.createStagingFile("anything").parentFile?.listFiles().orEmpty().map { it.name }.sorted()
private companion object {
val INPUT: Uri = Uri.parse("file:///tmp/holiday.mp4")