Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

fix(mobile): reserve only remaining download bytes

debpalash7a137d4a4aab
diff --git a/mobile/android/core/local/src/main/kotlin/com/voicestudio/mobile/local/ModelDownloads.kt b/mobile/android/core/local/src/main/kotlin/com/voicestudio/mobile/local/ModelDownloads.kt
index 370a79c01..6d6e3ab89 100644
--- a/mobile/android/core/local/src/main/kotlin/com/voicestudio/mobile/local/ModelDownloads.kt
+++ b/mobile/android/core/local/src/main/kotlin/com/voicestudio/mobile/local/ModelDownloads.kt
@@ -44,7 +44,7 @@ class ModelDownloads internal constructor(
}
private fun invalidate(model: PhoneModel) {model.files.forEach {verified.remove(File(dir(model),it.path))}}
private val locks=ConcurrentHashMap<String,Mutex>()
- private val reservations=HashMap<String,Long>()
+ private val reservations=HashMap<String,()->Long>()
private fun lock(model: PhoneModel)=locks.getOrPut(model.id) {Mutex()}
private val mutable=MutableStateFlow<Map<String,DownloadProgress>>(emptyMap())
val progress=mutable.asStateFlow()
@@ -64,16 +64,18 @@ class ModelDownloads internal constructor(
val job=currentCoroutineContext().job
val directory=dir(model).apply {mkdirs()}
val catalogue=modelPin(model).identity
- val completedFiles=model.files.filter {verifiedFile(File(directory,it.path),it,catalogue) {job.ensureActive()}}.toSet()
- val needed=model.files.filterNot {it in completedFiles}.sumOf { f ->
+ val completedFiles=model.files.filter {verifiedFile(File(directory,it.path),it,catalogue) {job.ensureActive()}}.toMutableSet()
+ fun remainingBytes()=model.files.filterNot {it in completedFiles}.sumOf { f ->
val partial=File(directory,f.path+".part").length().takeIf {it<=f.size} ?: 0L
f.size-partial
}
+ val needed=remainingBytes()
if(needed>0) networkAllowed(allowMobile)
- // Concurrent downloads each pass the check alone; reserve what the others still need.
+ // Read current partial lengths so retries and completed writes are not counted twice.
+ // completedFiles is updated under this same monitor after each successful install.
synchronized(reservations) {
- if(needed>0 && freeBytes()-reservations.values.sum()<needed+16*1024*1024) throw ClientFailure("mobile.local.storage_required")
- reservations[model.id]=needed
+ if(needed>0 && freeBytes()-reservations.values.sumOf {it()}<needed+16*1024*1024) throw ClientFailure("mobile.local.storage_required")
+ reservations[model.id]=::remainingBytes
}
mutable.update {it+(model.id to DownloadProgress(total=model.totalBytes,running=true))}
try {
@@ -96,7 +98,10 @@ class ModelDownloads internal constructor(
currentCoroutineContext().ensureActive()
if(part.length()!=f.size||digest(part) {job.ensureActive()}!=f.sha256) {part.delete();throw ClientFailure("mobile.local.checksum_failed")}
licence.requireAccepted(model)
- if(!part.renameTo(target)) throw ClientFailure("mobile.local.storage_required")
+ synchronized(reservations) {
+ if(!part.renameTo(target)) throw ClientFailure("mobile.local.storage_required")
+ completedFiles.add(f)
+ }
verified[target]=stamp(target,catalogue)
completed+=f.size
}
diff --git a/mobile/android/core/local/src/test/kotlin/com/voicestudio/mobile/local/LocalPolicyTest.kt b/mobile/android/core/local/src/test/kotlin/com/voicestudio/mobile/local/LocalPolicyTest.kt
index 299d3dab8..dcacb0e02 100644
--- a/mobile/android/core/local/src/test/kotlin/com/voicestudio/mobile/local/LocalPolicyTest.kt
+++ b/mobile/android/core/local/src/test/kotlin/com/voicestudio/mobile/local/LocalPolicyTest.kt
@@ -146,6 +146,21 @@ class LocalPolicyTest {
assertEquals(2,server.requestCount)
}
}
+ @Test fun concurrentDownloadCountsOnlyBytesStillMissingFromOtherDownloads() = runBlocking {
+ MockWebServer().use {server -> server.start()
+ server.enqueue(MockResponse().setBody(Buffer().write(payload)).throttleBody(payload.size-1L,2,TimeUnit.SECONDS))
+ server.enqueue(MockResponse().setBody(Buffer().write(payload)))
+ val part=File(temporary.root,"models/fixture/weights.bin.part")
+ val d=downloads(server,free={16*1024*1024+2L*payload.size-part.length()})
+ val first=launch(Dispatchers.IO) {d.download(model(),false)}
+ try {
+ withTimeout(5000) {while(part.length()<payload.size-1) delay(10)}
+ withTimeout(5000) {d.download(model().copy(id="second"),false)}
+ assertTrue(d.installed(model().copy(id="second")))
+ assertEquals(2,server.requestCount)
+ } finally {first.cancelAndJoin()}
+ }
+ }
@Test fun withdrawalBlocksPreviouslyVerifiedWeights() = runTest {
MockWebServer().use {server ->server.start();server.enqueue(MockResponse().setBody(Buffer().write(payload)))
val m=model(true);val g=gate();g.record(m,true);val d=downloads(server,g);d.download(m,false)