mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-17 19:36:28 +02:00
The object list lives in PrinterState and is filled by the print-start path, which bambu_mqtt suppresses on the first RUNNING push after startup so a running print is not archived twice (#1304). Everything else that moment restores came back - the archive into _active_prints, the filament attribution session, the timelapse baseline - and the object list did not. So the card saw zero objects and greyed out its Skip button for the rest of the print. Measured on the maintainer's H2C: 8 objects loaded at 09:02, a restart at 09:17, Skip dead for the remaining hour. Nothing could bring it back either. GET /print/objects rebuilds the list whenever it is empty, but its only caller is the modal that the greyed-out button opens. on_print_running_observed now reloads the objects from the archive of the print that is still running, anchored on subtask_id - the firmware mints one per print, so a leftover status="printing" row from a completion that was never seen cannot lend its objects to another job. Without an id nothing is loaded rather than guessed; the endpoint's own reload covers that on demand. That endpoint now reads the archived 3MF from disk before it asks the printer. The archive of a running print normally holds the very file the printer is executing, so the fan-out was fetching back 15 MB Bambuddy already had, over the printer's single FTP socket, while it was printing - and on a printer that kept the file on internal storage it cannot succeed at all. FTP stays as the fallback. skipped_objects is left alone: a reload is not a new print, and what the user has already skipped only lives there. The plate image had the same fault one layer down. Opening the modal asks for the cover, the top view and the object-ID mask, and the in-memory 3MF cache those share dies with the process - so after a restart all three went back to the printer at once: three fan-outs, thirteen seconds, and a 0-byte read from socket contention, which is the storm #972 was about. The cover flow takes the running print's archived file too, resolved in the caller's short-lived session and passed in so _produce_cover_image still does no DB work, and marked as a shared file so the cleanup cannot delete the archive. Finally the card: a running print always has at least one object, so a count of zero means "not loaded", not "nothing to skip". Exactly one object is the real nothing-to-skip case and still disables the button. --- Stop a test's printer client leaking into the next test POST /api/v1/printers really connects, so a test that creates a printer through the API leaves a live client in the printer_manager singleton. The singleton outlives the per-test in-memory database, so the next test on that xdist worker - whose own first printer is handed the same primary key - reads that leftover client as its own live status. test_scheduled_drying_routes was the visible victim: an "online" printer with no firmware version fails the drying preflight, so scheduling came back 400 instead of 200. It only bites when --dist load happens to put victim and leaker on one worker, which is why it passes on its own and flakes under -n. Registrations made during a test are now undone after it, ids the test did not add are left alone, and disconnect_printer is what also drops the model and printer-info caches and stops the paho thread the leaked client was keeping alive against an unreachable address for the rest of the run.
97 lines
3.8 KiB
Python
97 lines
3.8 KiB
Python
"""Concurrent cover requests for the same print coalesce into one download (#2572).
|
|
|
|
The farm dashboard mounts a cover tile per printer card, so several browsers
|
|
request the same printer's cover in the same instant. Before this fix each miss
|
|
ran the full multi-path FTP lookup + 3MF extraction independently (one observed
|
|
live transfer pulled an 81 MB 3MF while real uploads were in flight). Now the
|
|
first miss becomes the leader and the rest await its result, then serve from the
|
|
cache it filled.
|
|
"""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import backend.app.api.routes.printers as printers_mod
|
|
from backend.app.api.routes.printers import get_printer_cover
|
|
|
|
|
|
class _FakeSession:
|
|
def __init__(self, printer):
|
|
self._printer = printer
|
|
|
|
async def __aenter__(self):
|
|
return self
|
|
|
|
async def __aexit__(self, *exc):
|
|
return False
|
|
|
|
async def execute(self, *args, **kwargs):
|
|
return SimpleNamespace(scalar_one_or_none=lambda: self._printer)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clear_cover_state():
|
|
printers_mod._cover_cache.clear()
|
|
printers_mod._cover_404_cache.clear()
|
|
printers_mod._cover_inflight.clear()
|
|
yield
|
|
printers_mod._cover_cache.clear()
|
|
printers_mod._cover_404_cache.clear()
|
|
printers_mod._cover_inflight.clear()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_concurrent_cover_requests_download_once():
|
|
printer = SimpleNamespace(id=1, ip_address="127.0.0.1", access_code="x", model="X1C", name="P")
|
|
state = SimpleNamespace(subtask_name="job", state="RUNNING")
|
|
|
|
produce_calls = {"n": 0}
|
|
|
|
async def slow_produce(
|
|
printer_row, printer_id, subtask_name, view, view_key, plate_num, cache_key, archive_path=None
|
|
):
|
|
produce_calls["n"] += 1
|
|
await asyncio.sleep(0.1) # hold leadership long enough for followers to attach
|
|
printers_mod._cover_cache.setdefault(printer_id, {})[cache_key] = b"PNGDATA"
|
|
return b"PNGDATA"
|
|
|
|
with (
|
|
patch("backend.app.core.database.async_session", lambda: _FakeSession(printer)),
|
|
patch.object(printers_mod.printer_manager, "get_status", MagicMock(return_value=state)),
|
|
patch.object(printers_mod, "resolve_plate_id", MagicMock(return_value=1)),
|
|
patch.object(printers_mod, "_produce_cover_image", slow_produce),
|
|
):
|
|
responses = await asyncio.gather(*[get_printer_cover(1, None, None) for _ in range(5)])
|
|
|
|
assert produce_calls["n"] == 1, "concurrent cover requests each ran their own FTP download"
|
|
assert {bytes(r.body) for r in responses} == {b"PNGDATA"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_second_request_serves_from_positive_cache():
|
|
"""A follower arriving after the leader filled the cache serves it directly."""
|
|
printer = SimpleNamespace(id=1, ip_address="127.0.0.1", access_code="x", model="X1C", name="P")
|
|
state = SimpleNamespace(subtask_name="job", state="RUNNING")
|
|
|
|
produce_calls = {"n": 0}
|
|
|
|
async def produce(printer_row, printer_id, subtask_name, view, view_key, plate_num, cache_key, archive_path=None):
|
|
produce_calls["n"] += 1
|
|
printers_mod._cover_cache.setdefault(printer_id, {})[cache_key] = b"PNGDATA"
|
|
return b"PNGDATA"
|
|
|
|
with (
|
|
patch("backend.app.core.database.async_session", lambda: _FakeSession(printer)),
|
|
patch.object(printers_mod.printer_manager, "get_status", MagicMock(return_value=state)),
|
|
patch.object(printers_mod, "resolve_plate_id", MagicMock(return_value=1)),
|
|
patch.object(printers_mod, "_produce_cover_image", produce),
|
|
):
|
|
first = await get_printer_cover(1, None, None)
|
|
second = await get_printer_cover(1, None, None)
|
|
|
|
assert produce_calls["n"] == 1 # second hit the positive cache
|
|
assert bytes(first.body) == bytes(second.body) == b"PNGDATA"
|