"""STL thumbnails are rendered off the event loop (RC-C12). trimesh loads the mesh and matplotlib draws it, which takes seconds on a high-poly file. Four library paths ran that on the event loop, so an upload, a ZIP of STLs, a batch "generate thumbnails" or an external folder scan froze every request and printer update until it finished. They now render in a worker thread, like the Manyfold import already did, and the renderer holds a lock: matplotlib's pyplot is not thread-safe, and two renders at once (a combine and an import, say) could draw into each other's figure. """ import contextlib import io import struct import threading import zipfile import pytest from httpx import AsyncClient from backend.app.api.routes import library from backend.app.services import stl_thumbnail def _binary_stl(triangles: int = 8) -> bytes: """A binary STL comfortably above MIN_USABLE_STL_BYTES.""" header = b"bambuddy test".ljust(80, b"\0") body = b"" for i in range(triangles): body += struct.pack("<12fH", 0, 0, 1, 0, 0, i, 1, 0, i, 0, 1, i, 0) return header + struct.pack("