harry0703/MoneyPrinterTurbo · error · ValueError

background music file does not exist

Error message

background music file does not exist

What it means

The default 'background music file does not exist' ValueError is the fallback raised by resolve_bgm_file when every candidate path failed against both whitelisted directories (uploaded_bgm_dir and utils.song_dir()). Each failure came from file_security.resolve_path_within_directory, so the actually raised message is the LAST error — typically 'file does not exist' or 'path is outside the allowed directory'. Relative paths are tried both as-is and joined with the project root, and the user upload directory wins over built-in songs on name collision.

Source

Thrown at app/services/bgm.py:328

    """
    if (
        not unsafe_path
        or Path(unsafe_path).suffix.lower() not in SUPPORTED_BGM_EXTENSIONS
    ):
        raise ValueError("unsupported background music path")

    candidates = [unsafe_path]
    if not os.path.isabs(unsafe_path):
        candidates.append(os.path.join(utils.root_dir(), unsafe_path))

    last_error = ValueError("background music file does not exist")
    for directory in (uploaded_bgm_dir(create=True), utils.song_dir()):
        for candidate in candidates:
            try:
                return file_security.resolve_path_within_directory(directory, candidate)
            except ValueError as exc:
                last_error = exc
    raise ValueError(str(last_error)) from last_error

View on GitHub (pinned to 1f9f19c202)

Solutions

  1. Confirm the file actually exists under storage/bgm/ (user uploads) or resource/songs/ (built-ins) on the machine running the app.
  2. Re-upload the BGM or pick a currently listed track; update the saved task/config to reference an existing file.
  3. If the path was absolute, switch to a bare filename so it resolves against the whitelisted directories.

Example fix

// before
resolve_bgm_file('/home/user/music/track.mp3')  # outside whitelists

// after
resolve_bgm_file('track.mp3')  # resolves within storage/bgm or resource/songs
Defensive patterns

Strategy: validation

Validate before calling

import os
from app.services.bgm import uploaded_bgm_dir
from app.utils import utils

def bgm_exists(unsafe_name: str) -> bool:
    for d in (uploaded_bgm_dir(create=False), utils.song_dir()):
        if os.path.isfile(os.path.join(d, unsafe_name)):
            return True
    return False

Try / catch

try:
    resolved = resolve_bgm_file(path)
except ValueError as e:
    # 'file does not exist' or 'path is outside the allowed directory'
    fallback_to_no_bgm(); warn_user(f'BGM unavailable: {e}')

Prevention

When it happens

Trigger: Passing a filename that exists in neither storage/bgm/ nor resource/songs/, or an absolute/relative path that escapes those directories (then the message becomes 'path is outside the allowed directory'). Both the raw candidate and the root-joined candidate must fail for this to fire.

Common situations: Storage volume not mounted (Docker rebuild lost storage/bgm), file deleted by cleanup while config still references it, absolute path from another machine, traversal attempts like '../../etc/passwd'.

Related errors


AI-assisted analysis of harry0703/MoneyPrinterTurbo@1f9f19c202 (2026-08-14). Data as JSON: /api/errors/5028469b6a313da3. Report an issue: GitHub.