From 54c043c12703639e3d14ab9e10368dfd65c507d7 Mon Sep 17 00:00:00 2001 From: loucass003 Date: Tue, 27 Oct 2026 22:21:55 +0100 Subject: [PATCH] Add move file retry when saving files. Windows moment.... --- .../dev/slimevr/desktop/config/storage.kt | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/config/storage.kt b/server/desktop/src/main/java/dev/slimevr/desktop/config/storage.kt index 5dfc8a013..cc06652c6 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/config/storage.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/config/storage.kt @@ -3,12 +3,34 @@ package dev.slimevr.desktop.config import dev.slimevr.config.ConfigStorage import dev.slimevr.config.TextFileHandle import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.withContext import java.io.File import java.io.RandomAccessFile +import java.nio.file.AccessDeniedException +import java.nio.file.CopyOption import java.nio.file.FileAlreadyExistsException import java.nio.file.Files +import java.nio.file.Path import java.nio.file.StandardCopyOption +import kotlin.time.Duration.Companion.milliseconds + +private const val MOVE_ATTEMPTS = 5 +private val MOVE_RETRY_BASE_DELAY_MS = 10.milliseconds + +// Windows denies a rename while another process (antivirus, sync, indexer) holds either file open +// mimicking a simple fix ive seen in some node lib a long time ago +private suspend fun moveWithRetry(source: Path, target: Path, vararg options: CopyOption) = withContext(Dispatchers.IO) { + repeat(MOVE_ATTEMPTS) { attempt -> + try { + Files.move(source, target, *options) + return@withContext + } catch (e: AccessDeniedException) { + if (attempt == MOVE_ATTEMPTS - 1) throw e + delay(MOVE_RETRY_BASE_DELAY_MS * (attempt + 1)) + } + } +} class DesktopConfigStorage( private val root: File, @@ -24,11 +46,11 @@ class DesktopConfigStorage( val tmp = File(file.parent, "${file.name}.tmp") tmp.writeText(content) if (overwrite) { - Files.move(tmp.toPath(), file.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) + moveWithRetry(tmp.toPath(), file.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) return@withContext true } try { - Files.move(tmp.toPath(), file.toPath()) + moveWithRetry(tmp.toPath(), file.toPath()) true } catch (_: FileAlreadyExistsException) { tmp.delete() @@ -41,7 +63,7 @@ class DesktopConfigStorage( if (!file.exists()) return@withContext val bakTmp = File(file.parent, "${file.name}.bak.tmp") file.copyTo(bakTmp, overwrite = true) - Files.move( + moveWithRetry( bakTmp.toPath(), File(file.parent, "${file.name}.bak").toPath(), StandardCopyOption.ATOMIC_MOVE,