From ccfc5dedd4ea7ebfd2412f88d8983e71dbe8485b Mon Sep 17 00:00:00 2001 From: Luke Mino-Altherr Date: Tue, 3 Feb 2026 14:46:33 -0800 Subject: [PATCH] fix: handle missing blake3 module gracefully to prevent server crash Make blake3 an optional import that fails gracefully at import time, with a clear error message when hashing functions are actually called. Co-Authored-By: Claude Opus 4.5 --- app/assets/services/hashing.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/assets/services/hashing.py b/app/assets/services/hashing.py index 4b8db4e9b..8efd3adf9 100644 --- a/app/assets/services/hashing.py +++ b/app/assets/services/hashing.py @@ -1,11 +1,24 @@ -from blake3 import blake3 from typing import IO import os import asyncio +try: + from blake3 import blake3 +except ImportError: + blake3 = None # type: ignore[misc, assignment] + DEFAULT_CHUNK = 8 * 1024 *1024 # 8MB + +def _require_blake3() -> None: + """Raise ImportError with helpful message if blake3 is not installed.""" + if blake3 is None: + raise ImportError( + "blake3 is required for asset hashing but is not installed. " + "Install it with: pip install blake3" + ) + # NOTE: this allows hashing different representations of a file-like object def compute_blake3_hash( fp: str | IO[bytes], @@ -51,6 +64,8 @@ def _hash_file_obj(file_obj: IO, chunk_size: int = DEFAULT_CHUNK) -> str: - Seeks to the beginning before reading (if supported). - Restores the original position afterward (if tell/seek are supported). """ + _require_blake3() + if chunk_size <= 0: chunk_size = DEFAULT_CHUNK