romm icon indicating copy to clipboard operation
romm copied to clipboard

[Bug] `/api/tasks/status` returns 500 error after a "Cleanup missing ROMs" task completes

Open pacnpal opened this issue 8 months ago • 0 comments

RomM version 4.8.0 (Docker, latest tag)

Describe the bug The /api/tasks/status endpoint returns a 500 Internal Server Error after a "Cleanup missing ROMs" task completes. The cleanup task itself runs successfully (deletes orphaned ROMs as expected), but when the administration page polls /api/tasks/status, FastAPI's response serialization fails with 14 validation errors because the cleanup_stats object returned by the task does not match the CleanupTaskStatusResponse Pydantic model.

The task writes a cleanup_stats dict containing only:

  • platform_id
  • roms_found
  • roms_deleted
  • errors

But CleanupTaskStatusResponse expects six additional required fields:

  • platforms_in_db
  • roms_in_db
  • platforms_in_fs
  • roms_in_fs
  • removed_fs_platforms
  • removed_fs_roms

Because the endpoint returns a discriminated union of task types and the cleanup variant fails validation, all task status polling breaks — the entire /administration page becomes unusable. The malformed job result persists in Redis (via RQ) with a result_ttl of 86400, so the 500 continues for up to 24 hours until the key expires.

To Reproduce

  1. Have ROMs in your library that are flagged as "missing from filesystem"
  2. Run (or let the scheduler run) the "Cleanup missing ROMs" task
  3. Task completes successfully and deletes the missing ROMs
  4. Navigate to /administration
  5. The page polls /api/tasks/status every ~5 seconds and receives a 500 on every request

Expected behavior The /api/tasks/status endpoint should return a valid JSON response after cleanup tasks complete. The cleanup task handler should populate all required fields in cleanup_stats, or the missing fields should be made optional in the CleanupTaskStatusResponse schema.

Screenshots N/A — the administration page just shows a generic error. The server logs show the full traceback (see Additional context).

Server (please complete the following information):

  • OS: Unraid
  • RomM version: 4.8.0 (Docker)
  • Database: MariaDB
  • Redis: Bundled Valkey

Client (please complete the following information):

  • Device: MacBook
  • OS: macOS
  • Browser: Firefox
  • Version: 148.0.2

Additional context The full ResponseValidationError traceback from the server logs:

fastapi.exceptions.ResponseValidationError: 14 validation errors:
  {'type': 'literal_error', 'loc': ('response', 1, 'ScanTaskStatusResponse', 'task_type'), 'msg': "Input should be <TaskType.SCAN: 'scan'>", 'input': <TaskType.CLEANUP: 'cleanup'>, 'ctx': {'expected': "<TaskType.SCAN: 'scan'>"}}
  {'type': 'missing', 'loc': ('response', 1, 'ScanTaskStatusResponse', 'meta', 'scan_stats'), 'msg': 'Field required', 'input': {'cleanup_stats': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}}
  {'type': 'literal_error', 'loc': ('response', 1, 'ConversionTaskStatusResponse', 'task_type'), 'msg': "Input should be <TaskType.CONVERSION: 'conversion'>", 'input': <TaskType.CLEANUP: 'cleanup'>, 'ctx': {'expected': "<TaskType.CONVERSION: 'conversion'>"}}
  {'type': 'missing', 'loc': ('response', 1, 'ConversionTaskStatusResponse', 'meta', 'conversion_stats'), 'msg': 'Field required', 'input': {'cleanup_stats': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}}
  {'type': 'literal_error', 'loc': ('response', 1, 'UpdateTaskStatusResponse', 'task_type'), 'msg': "Input should be <TaskType.UPDATE: 'update'>", 'input': <TaskType.CLEANUP: 'cleanup'>, 'ctx': {'expected': "<TaskType.UPDATE: 'update'>"}}
  {'type': 'missing', 'loc': ('response', 1, 'UpdateTaskStatusResponse', 'meta', 'update_stats'), 'msg': 'Field required', 'input': {'cleanup_stats': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'platforms_in_db'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'roms_in_db'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'platforms_in_fs'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'roms_in_fs'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'removed_fs_platforms'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'missing', 'loc': ('response', 1, 'CleanupTaskStatusResponse', 'meta', 'cleanup_stats', 'removed_fs_roms'), 'msg': 'Field required', 'input': {'platform_id': None, 'roms_found': 10, 'roms_deleted': 10, 'errors': 0}}
  {'type': 'literal_error', 'loc': ('response', 1, 'WatcherTaskStatusResponse', 'task_type'), 'msg': "Input should be <TaskType.WATCHER: 'watcher'>", 'input': <TaskType.CLEANUP: 'cleanup'>, 'ctx': {'expected': "<TaskType.WATCHER: 'watcher'>"}}
  {'type': 'literal_error', 'loc': ('response', 1, 'GenericTaskStatusResponse', 'task_type'), 'msg': "Input should be <TaskType.GENERIC: 'generic'>", 'input': <TaskType.CLEANUP: 'cleanup'>, 'ctx': {'expected': "<TaskType.GENERIC: 'generic'>"}}

The root cause appears to be that the "Cleanup missing ROMs" task handler populates cleanup_stats with a different (smaller) set of fields than what the CleanupTaskStatusResponse schema requires. The "Cleanup orphaned resources" task may have the same issue but I didn't verify separately.

Workaround: Delete the offending RQ job and result keys from Redis manually using python3 -c "import redis; r = redis.Redis(); ..." from inside the container (since valkey-cli is not bundled). The job has a result_ttl of 86400 so it will also self-resolve after 24 hours, but the administration page is completely broken in the meantime.

pacnpal avatar Apr 01 '26 13:04 pacnpal