From 86b6f45f75fc2d8dea8e8f2a66d7c8eb42c158f7 Mon Sep 17 00:00:00 2001 From: nendo Date: Mon, 31 Aug 2026 13:43:37 +0900 Subject: [PATCH] fix(roms): stop a racing upload from undoing a folder conversion Uploading several files fires a request per file, and each one promotes the same single-file ROM. Both read it as single-file, both clear the collision check, then one moves the ROM into the new folder and the other finds nothing at the old path. Its rollback assumed the file at the destination was its own and moved it back out, leaving the ROM beside the folder while the database recorded the conversion. Promotions for a ROM now take a lock and re-read it, so the second request sees a folder ROM and returns instead of racing. --- backend/handler/rom_conversion.py | 11 +++++ .../endpoints/roms/test_convert_to_folder.py | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/backend/handler/rom_conversion.py b/backend/handler/rom_conversion.py index afc8bdd12..ab854d990 100644 --- a/backend/handler/rom_conversion.py +++ b/backend/handler/rom_conversion.py @@ -1,3 +1,6 @@ +import asyncio +from collections import defaultdict + from exceptions.fs_exceptions import RomAlreadyExistsException from handler.database import db_rom_handler from handler.filesystem import fs_rom_handler @@ -8,12 +11,20 @@ from models.rom import Rom _STAGE_PREFIX = ".romm_tmp_" +# Uploading several files fires a request per file, each promoting the same ROM. +_promotion_locks: defaultdict[int, asyncio.Lock] = defaultdict(asyncio.Lock) + async def promote_single_file_to_folder(rom: Rom) -> Rom: """Promote a simple single-file ROM to a folder ROM in place, keeping rom.id and every relation. Idempotent; raises RomAlreadyExistsException on a folder-name collision. """ + async with _promotion_locks[rom.id]: + return await _promote(db_rom_handler.get_rom(rom.id) or rom) + + +async def _promote(rom: Rom) -> Rom: if not rom.has_simple_single_file: return rom diff --git a/backend/tests/endpoints/roms/test_convert_to_folder.py b/backend/tests/endpoints/roms/test_convert_to_folder.py index 489601e0c..d9699254d 100644 --- a/backend/tests/endpoints/roms/test_convert_to_folder.py +++ b/backend/tests/endpoints/roms/test_convert_to_folder.py @@ -1,3 +1,4 @@ +import asyncio from pathlib import Path import pytest @@ -6,6 +7,7 @@ from fastapi.testclient import TestClient from handler.database import db_rom_handler from handler.filesystem import fs_rom_handler +from handler.rom_conversion import promote_single_file_to_folder from models.platform import Platform from models.rom import Rom, RomFile, RomFileCategory from models.user import User @@ -341,3 +343,44 @@ def test_screenshot_upload_auto_converts_single_file_rom( after = db_rom_handler.get_rom(rom.id) assert after.fs_name == "test_rom" assert any(f.category == RomFileCategory.SCREENSHOT for f in after.files) + + +async def test_second_upload_racing_a_promotion_keeps_the_rom_in_its_folder( + platform: Platform, + admin_user: User, + real_library: Path, + monkeypatch: pytest.MonkeyPatch, +): + rom = _single_file_rom( + platform, + admin_user, + real_library, + fs_name="sf2ce.zip", + fs_name_no_ext="sf2ce", + fs_extension="zip", + ) + second = db_rom_handler.get_rom(rom.id) + + # Holding both at the mkdir puts them past the collision check together. + make_directory = fs_rom_handler.make_directory + barrier = asyncio.Barrier(2) + + async def gated_make_directory(path: str) -> None: + try: + await asyncio.wait_for(barrier.wait(), 0.5) + except (TimeoutError, asyncio.BrokenBarrierError): + pass + await make_directory(path) + + monkeypatch.setattr(fs_rom_handler, "make_directory", gated_make_directory) + + await asyncio.gather( + promote_single_file_to_folder(rom), + promote_single_file_to_folder(second), + return_exceptions=True, + ) + + inside = real_library / f"{platform.slug}/roms/sf2ce/sf2ce.zip" + beside = real_library / f"{platform.slug}/roms/sf2ce.zip" + assert inside.exists(), "the loser dragged the ROM back out of its folder" + assert not beside.exists()