mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
simplify: emit negotiate conflicts sequentially under one deadline
With the emits off the response path, the semaphore and gather fan-out bought nothing. The conflict tests share the module-level negotiate helper and an emit patch, and the toast tests share their install and fire setup. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -42,10 +42,8 @@ router = APIRouter(
|
||||
tags=["sync"],
|
||||
)
|
||||
|
||||
# Each in-flight emit holds its own Redis connection, so a wide conflict set is
|
||||
# bounded two ways: a deadline on the wait, and a cap on concurrent emits.
|
||||
# A hung broker must not pin the background task forever.
|
||||
CONFLICT_NOTIFY_TIMEOUT_S = 2.0
|
||||
CONFLICT_NOTIFY_MAX_CONCURRENCY = 8
|
||||
|
||||
|
||||
class ClientSaveState(BaseModel):
|
||||
@@ -128,40 +126,26 @@ async def _notify_conflicts(
|
||||
session_id: int,
|
||||
conflict_ops: list[SyncOperationSchema],
|
||||
) -> None:
|
||||
"""Emit one sync:conflict event per operation, batched and bounded."""
|
||||
limiter = asyncio.Semaphore(CONFLICT_NOTIFY_MAX_CONCURRENCY)
|
||||
|
||||
async def emit_one(op: SyncOperationSchema) -> None:
|
||||
async with limiter:
|
||||
await emit_sync_conflict(
|
||||
user_id=user_id,
|
||||
device_id=device_id,
|
||||
session_id=session_id,
|
||||
file_name=op.file_name,
|
||||
rom_id=op.rom_id,
|
||||
reason=op.reason,
|
||||
)
|
||||
|
||||
"""Emit one sync:conflict event per operation, within one deadline."""
|
||||
try:
|
||||
results = await asyncio.wait_for(
|
||||
asyncio.gather(
|
||||
*(emit_one(op) for op in conflict_ops),
|
||||
return_exceptions=True,
|
||||
),
|
||||
timeout=CONFLICT_NOTIFY_TIMEOUT_S,
|
||||
)
|
||||
for op, emit_result in zip(conflict_ops, results, strict=True):
|
||||
if isinstance(emit_result, BaseException):
|
||||
log.warning(
|
||||
f"Failed to emit sync:conflict for {op.file_name}: {emit_result}"
|
||||
)
|
||||
async with asyncio.timeout(CONFLICT_NOTIFY_TIMEOUT_S):
|
||||
for op in conflict_ops:
|
||||
try:
|
||||
await emit_sync_conflict(
|
||||
user_id=user_id,
|
||||
device_id=device_id,
|
||||
session_id=session_id,
|
||||
file_name=op.file_name,
|
||||
rom_id=op.rom_id,
|
||||
reason=op.reason,
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
log.warning(f"Failed to emit sync:conflict for {op.file_name}: {e}")
|
||||
except TimeoutError:
|
||||
log.warning(
|
||||
f"Gave up on {len(conflict_ops)} sync:conflict events "
|
||||
f"after {CONFLICT_NOTIFY_TIMEOUT_S}s"
|
||||
)
|
||||
except Exception as e: # noqa: BLE001
|
||||
log.warning(f"Failed to emit {len(conflict_ops)} sync:conflict events: {e}")
|
||||
|
||||
|
||||
@protected_route(router.post, "/negotiate", [Scope.ASSETS_READ, Scope.DEVICES_READ])
|
||||
|
||||
@@ -573,6 +573,16 @@ class TestPushPullTrigger:
|
||||
assert "session_id" in call_kwargs.kwargs
|
||||
|
||||
|
||||
def _negotiate(client, access_token, device_id, saves):
|
||||
resp = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={"device_id": device_id, "saves": saves},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
assert resp.status_code == status.HTTP_200_OK
|
||||
return resp.json()
|
||||
|
||||
|
||||
class TestNegotiateAdvanced:
|
||||
def test_negotiate_untracked_save_returns_noop(
|
||||
self, client, access_token: str, admin_user: User, save: Save
|
||||
@@ -772,15 +782,6 @@ class TestNegotiateAdvanced:
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
|
||||
def _negotiate(self, client, access_token, device_id, saves):
|
||||
resp = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={"device_id": device_id, "saves": saves},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
assert resp.status_code == status.HTTP_200_OK
|
||||
return resp.json()
|
||||
|
||||
@staticmethod
|
||||
def _autosave_entry(rom, content_hash):
|
||||
return {
|
||||
@@ -812,7 +813,7 @@ class TestNegotiateAdvanced:
|
||||
assert stored["file_name"] != "pokemon_violet.zip"
|
||||
assert " [" in stored["file_name"]
|
||||
|
||||
data = self._negotiate(
|
||||
data = _negotiate(
|
||||
client, access_token, device.id, [self._autosave_entry(rom, "HASH_RT")]
|
||||
)
|
||||
assert data["total_upload"] == 0
|
||||
@@ -839,7 +840,7 @@ class TestNegotiateAdvanced:
|
||||
assert up.status_code == status.HTTP_200_OK
|
||||
save_id = up.json()["id"]
|
||||
|
||||
a_data = self._negotiate(
|
||||
a_data = _negotiate(
|
||||
client, access_token, device_a.id, [self._autosave_entry(rom, "HASH_3D")]
|
||||
)
|
||||
assert a_data["total_upload"] == 0
|
||||
@@ -849,7 +850,7 @@ class TestNegotiateAdvanced:
|
||||
dev = db_device_handler.add_device(
|
||||
Device(id=dev_id, user_id=admin_user.id, sync_enabled=True)
|
||||
)
|
||||
first = self._negotiate(client, access_token, dev.id, [])
|
||||
first = _negotiate(client, access_token, dev.id, [])
|
||||
downloads = [
|
||||
op
|
||||
for op in first["operations"]
|
||||
@@ -859,7 +860,7 @@ class TestNegotiateAdvanced:
|
||||
db_device_save_sync_handler.upsert_sync(
|
||||
device_id=dev.id, save_id=save_id, synced_at=datetime.now(timezone.utc)
|
||||
)
|
||||
second = self._negotiate(
|
||||
second = _negotiate(
|
||||
client, access_token, dev.id, [self._autosave_entry(rom, "HASH_3D")]
|
||||
)
|
||||
assert second["total_upload"] == 0
|
||||
@@ -1034,16 +1035,19 @@ class TestNegotiateConflictEvents:
|
||||
event is the only surface the user gets."""
|
||||
|
||||
@staticmethod
|
||||
def _device_with_history(device_id: str, admin_user: User, save: Save) -> Device:
|
||||
"""A device whose last sync of `save` was an hour ago."""
|
||||
def _device_with_history(
|
||||
device_id: str, admin_user: User, saves: list[Save]
|
||||
) -> Device:
|
||||
"""A device whose last sync of each save was an hour ago."""
|
||||
device = db_device_handler.add_device(
|
||||
Device(id=device_id, user_id=admin_user.id, sync_enabled=True)
|
||||
)
|
||||
db_device_save_sync_handler.upsert_sync(
|
||||
device_id=device.id,
|
||||
save_id=save.id,
|
||||
synced_at=datetime.now(timezone.utc) - timedelta(hours=1),
|
||||
)
|
||||
for save in saves:
|
||||
db_device_save_sync_handler.upsert_sync(
|
||||
device_id=device.id,
|
||||
save_id=save.id,
|
||||
synced_at=datetime.now(timezone.utc) - timedelta(hours=1),
|
||||
)
|
||||
return device
|
||||
|
||||
@staticmethod
|
||||
@@ -1057,25 +1061,24 @@ class TestNegotiateConflictEvents:
|
||||
"file_size_bytes": 100,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _patch_emit(side_effect: Any = None):
|
||||
return mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict",
|
||||
new_callable=mock.AsyncMock,
|
||||
side_effect=side_effect,
|
||||
)
|
||||
|
||||
def test_conflict_emits_socket_event(
|
||||
self, client, access_token: str, admin_user: User, save: Save
|
||||
):
|
||||
device = self._device_with_history("neg-conflict-dev", admin_user, save)
|
||||
device = self._device_with_history("neg-conflict-dev", admin_user, [save])
|
||||
|
||||
with mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict", new_callable=mock.AsyncMock
|
||||
) as emit:
|
||||
response = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={
|
||||
"device_id": device.id,
|
||||
"saves": [self._changed_client_save(save)],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
with self._patch_emit() as emit:
|
||||
data = _negotiate(
|
||||
client, access_token, device.id, [self._changed_client_save(save)]
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
data = response.json()
|
||||
assert data["total_conflict"] == 1
|
||||
emit.assert_awaited_once()
|
||||
assert emit.await_args is not None
|
||||
@@ -1099,20 +1102,11 @@ class TestNegotiateConflictEvents:
|
||||
device_id=device.id, save_id=save.id, untracked=True
|
||||
)
|
||||
|
||||
with mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict", new_callable=mock.AsyncMock
|
||||
) as emit:
|
||||
response = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={
|
||||
"device_id": device.id,
|
||||
"saves": [self._changed_client_save(save)],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
with self._patch_emit() as emit:
|
||||
data = _negotiate(
|
||||
client, access_token, device.id, [self._changed_client_save(save)]
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
data = response.json()
|
||||
assert data["total_conflict"] == 0
|
||||
assert any(op["action"] == "no_op" for op in data["operations"])
|
||||
emit.assert_not_awaited()
|
||||
@@ -1121,58 +1115,37 @@ class TestNegotiateConflictEvents:
|
||||
self, client, access_token: str, admin_user: User, save: Save
|
||||
):
|
||||
"""An unreachable Redis must not stop a client from syncing."""
|
||||
device = self._device_with_history("neg-conflict-down", admin_user, save)
|
||||
device = self._device_with_history("neg-conflict-down", admin_user, [save])
|
||||
|
||||
with mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict",
|
||||
new_callable=mock.AsyncMock,
|
||||
side_effect=RuntimeError("redis is down"),
|
||||
):
|
||||
response = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={
|
||||
"device_id": device.id,
|
||||
"saves": [self._changed_client_save(save)],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
with self._patch_emit(RuntimeError("redis is down")):
|
||||
data = _negotiate(
|
||||
client, access_token, device.id, [self._changed_client_save(save)]
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["total_conflict"] == 1
|
||||
assert data["total_conflict"] == 1
|
||||
|
||||
def test_slow_notification_does_not_stall_the_negotiation(
|
||||
def test_hung_broker_is_abandoned_at_the_deadline(
|
||||
self, client, access_token: str, admin_user: User, save: Save
|
||||
):
|
||||
"""A hung broker is abandoned at the deadline, not waited out."""
|
||||
device = self._device_with_history("neg-conflict-slow", admin_user, save)
|
||||
device = self._device_with_history("neg-conflict-slow", admin_user, [save])
|
||||
|
||||
async def hang(**_kwargs: Any) -> None:
|
||||
await asyncio.sleep(30)
|
||||
|
||||
with (
|
||||
mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict",
|
||||
new_callable=mock.AsyncMock,
|
||||
side_effect=hang,
|
||||
),
|
||||
self._patch_emit(hang),
|
||||
mock.patch("endpoints.sync.CONFLICT_NOTIFY_TIMEOUT_S", 0.01),
|
||||
):
|
||||
started = time.monotonic()
|
||||
response = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={
|
||||
"device_id": device.id,
|
||||
"saves": [self._changed_client_save(save)],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
data = _negotiate(
|
||||
client, access_token, device.id, [self._changed_client_save(save)]
|
||||
)
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["total_conflict"] == 1
|
||||
assert data["total_conflict"] == 1
|
||||
assert elapsed < 10
|
||||
|
||||
def test_wide_conflict_set_bounds_concurrent_emits(
|
||||
def test_one_failed_emit_does_not_drop_the_rest(
|
||||
self,
|
||||
client,
|
||||
access_token: str,
|
||||
@@ -1180,10 +1153,6 @@ class TestNegotiateConflictEvents:
|
||||
rom: Rom,
|
||||
platform: Platform,
|
||||
):
|
||||
"""Each in-flight emit holds a Redis connection, so the fan-out is capped."""
|
||||
device = db_device_handler.add_device(
|
||||
Device(id="neg-conflict-wide", user_id=admin_user.id, sync_enabled=True)
|
||||
)
|
||||
saves = [
|
||||
db_save_handler.add_save(
|
||||
Save(
|
||||
@@ -1199,44 +1168,17 @@ class TestNegotiateConflictEvents:
|
||||
file_size_bytes=1.0,
|
||||
)
|
||||
)
|
||||
for index in range(6)
|
||||
for index in range(3)
|
||||
]
|
||||
for save in saves:
|
||||
db_device_save_sync_handler.upsert_sync(
|
||||
device_id=device.id,
|
||||
save_id=save.id,
|
||||
synced_at=datetime.now(timezone.utc) - timedelta(hours=1),
|
||||
device = self._device_with_history("neg-conflict-wide", admin_user, saves)
|
||||
|
||||
with self._patch_emit([RuntimeError("redis blip"), None, None]) as emit:
|
||||
data = _negotiate(
|
||||
client,
|
||||
access_token,
|
||||
device.id,
|
||||
[self._changed_client_save(save) for save in saves],
|
||||
)
|
||||
|
||||
live = 0
|
||||
peak = 0
|
||||
cap = 2
|
||||
|
||||
async def track(**_kwargs: Any) -> None:
|
||||
nonlocal live, peak
|
||||
live += 1
|
||||
peak = max(peak, live)
|
||||
await asyncio.sleep(0.05)
|
||||
live -= 1
|
||||
|
||||
with (
|
||||
mock.patch(
|
||||
"endpoints.sync.emit_sync_conflict",
|
||||
new_callable=mock.AsyncMock,
|
||||
side_effect=track,
|
||||
) as emit,
|
||||
mock.patch("endpoints.sync.CONFLICT_NOTIFY_MAX_CONCURRENCY", cap),
|
||||
):
|
||||
response = client.post(
|
||||
"/api/sync/negotiate",
|
||||
json={
|
||||
"device_id": device.id,
|
||||
"saves": [self._changed_client_save(save) for save in saves],
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["total_conflict"] == len(saves)
|
||||
assert data["total_conflict"] == len(saves)
|
||||
assert emit.await_count == len(saves)
|
||||
assert peak == cap
|
||||
|
||||
@@ -73,6 +73,10 @@ function conflict(overrides: Record<string, unknown> = {}) {
|
||||
describe("useSyncConflictToast", () => {
|
||||
let toasts: unknown[] = [];
|
||||
|
||||
function fire(overrides: Record<string, unknown> = {}) {
|
||||
handlers.get("sync:conflict")?.(conflict(overrides));
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
handlers.clear();
|
||||
toasts = [];
|
||||
@@ -82,6 +86,7 @@ describe("useSyncConflictToast", () => {
|
||||
emitter.on("snackbarShow", (payload) => {
|
||||
toasts.push(payload);
|
||||
});
|
||||
install();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -90,8 +95,7 @@ describe("useSyncConflictToast", () => {
|
||||
});
|
||||
|
||||
it("warns with the game name", () => {
|
||||
install();
|
||||
handlers.get("sync:conflict")?.(conflict());
|
||||
fire();
|
||||
|
||||
expect(toasts).toHaveLength(1);
|
||||
expect(toasts[0]).toMatchObject({
|
||||
@@ -101,8 +105,7 @@ describe("useSyncConflictToast", () => {
|
||||
});
|
||||
|
||||
it("falls back to the file name when the ROM's name is blank", () => {
|
||||
install();
|
||||
handlers.get("sync:conflict")?.(conflict({ rom_id: blankNameRom.id }));
|
||||
fire({ rom_id: blankNameRom.id });
|
||||
|
||||
expect(toasts).toHaveLength(1);
|
||||
expect(toasts[0]).toMatchObject({
|
||||
@@ -111,8 +114,7 @@ describe("useSyncConflictToast", () => {
|
||||
});
|
||||
|
||||
it("falls back to the generic string when the ROM is not cached", () => {
|
||||
install();
|
||||
handlers.get("sync:conflict")?.(conflict({ rom_id: 999 }));
|
||||
fire({ rom_id: 999 });
|
||||
|
||||
expect(toasts).toHaveLength(1);
|
||||
expect(toasts[0]).toMatchObject({
|
||||
@@ -121,20 +123,16 @@ describe("useSyncConflictToast", () => {
|
||||
});
|
||||
|
||||
it("toasts once per conflict, however often it is reported", () => {
|
||||
install();
|
||||
const handler = handlers.get("sync:conflict");
|
||||
handler?.(conflict());
|
||||
handler?.(conflict());
|
||||
handler?.(conflict({ file_name: "another_game.sav" }));
|
||||
fire();
|
||||
fire();
|
||||
fire({ file_name: "another_game.sav" });
|
||||
|
||||
expect(toasts).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("toasts per device, so a second device's conflict is not swallowed", () => {
|
||||
install();
|
||||
const handler = handlers.get("sync:conflict");
|
||||
handler?.(conflict());
|
||||
handler?.(conflict({ device_id: "dev-2" }));
|
||||
fire();
|
||||
fire({ device_id: "dev-2" });
|
||||
|
||||
expect(toasts).toHaveLength(2);
|
||||
});
|
||||
|
||||
@@ -29,7 +29,6 @@ export function installSyncConflictToast(): void {
|
||||
galleryRomsStore.getRomById(romId) ??
|
||||
(romsStore.currentRom?.id === romId ? romsStore.currentRom : null) ??
|
||||
romsStore.recentRoms.find((recent) => recent.id === romId);
|
||||
// `name` is nullable and can be blank, so this is a truthy fallback.
|
||||
return rom ? rom.name || rom.fs_name : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,8 +59,7 @@ installPermissionsHydration();
|
||||
// route the user is on (navbar indicator + /scan view consume the same
|
||||
// store state).
|
||||
installScanLifecycle();
|
||||
// Global save-sync conflict wiring: a conflict detected by the server is
|
||||
// re-reported on every negotiation, so the toast dedupes per page session.
|
||||
// Save-sync conflicts reach the user from any route.
|
||||
installSyncConflictToast();
|
||||
// Saves and states a player could not hand over reach the server from any
|
||||
// route, so the next launch screen can offer them.
|
||||
|
||||
Reference in New Issue
Block a user