mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-21 10:15:04 +02:00
Restore the skip-objects list after a restart mid-print
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.
This commit is contained in:
+59
-19
@@ -2860,34 +2860,73 @@ async def _dispatch_user_print_email(
|
||||
def _load_objects_from_archive(archive, printer_id: int, logger) -> None:
|
||||
"""Extract printable objects from an archive's 3MF file and store in printer state."""
|
||||
try:
|
||||
from backend.app.services.archive import extract_printable_objects_from_3mf
|
||||
from backend.app.services.archive import extract_printable_objects_from_archive
|
||||
|
||||
client = printer_manager.get_client(printer_id)
|
||||
if not client:
|
||||
return
|
||||
|
||||
file_path = app_settings.base_dir / archive.file_path
|
||||
if file_path.is_file() and str(file_path).endswith(".3mf"):
|
||||
with open(file_path, "rb") as f:
|
||||
threemf_data = f.read()
|
||||
# Extract with positions for UI overlay, scoped to the plate that
|
||||
# is printing — resolve_plate_id is the same resolver /cover uses,
|
||||
# so the object list can't disagree with the thumbnail it is drawn
|
||||
# over (#2522).
|
||||
printable_objects, bbox_all = extract_printable_objects_from_3mf(
|
||||
threemf_data,
|
||||
plate_number=resolve_plate_id(client.state),
|
||||
include_positions=True,
|
||||
)
|
||||
if printable_objects:
|
||||
client.state.printable_objects = printable_objects
|
||||
client.state.printable_objects_bbox_all = bbox_all
|
||||
client.state.skipped_objects = []
|
||||
logger.info("Loaded %s printable objects for printer %s", len(printable_objects), printer_id)
|
||||
# Extract with positions for UI overlay, scoped to the plate that
|
||||
# is printing — resolve_plate_id is the same resolver /cover uses,
|
||||
# so the object list can't disagree with the thumbnail it is drawn
|
||||
# over (#2522).
|
||||
printable_objects, bbox_all = extract_printable_objects_from_archive(
|
||||
app_settings.base_dir / archive.file_path,
|
||||
plate_number=resolve_plate_id(client.state),
|
||||
)
|
||||
if printable_objects:
|
||||
client.state.printable_objects = printable_objects
|
||||
client.state.printable_objects_bbox_all = bbox_all
|
||||
client.state.skipped_objects = []
|
||||
logger.info("Loaded %s printable objects for printer %s", len(printable_objects), printer_id)
|
||||
except Exception as e:
|
||||
logger.debug("Failed to extract printable objects from archive: %s", e)
|
||||
|
||||
|
||||
async def _restore_printable_objects(printer_id: int, state, db, logger) -> None:
|
||||
"""Put the skip-objects list back after a restart mid-print.
|
||||
|
||||
``PrinterState.printable_objects`` is in-memory only, and the only thing
|
||||
that fills it is ``_load_objects_from_archive`` on the print-start paths —
|
||||
which the #1304 guard suppresses on the first RUNNING push after startup.
|
||||
Everything else this hook restores (the archive, the usage-tracking session,
|
||||
the timelapse baseline) was already handled; the object list was not, so a
|
||||
restart mid-print took skip-objects away for the rest of that print.
|
||||
|
||||
Nothing recovered it either: the printer card gates its Skip button on the
|
||||
object count, and the one endpoint that can rebuild the list is reachable
|
||||
only from the modal that button opens.
|
||||
|
||||
Anchored on ``subtask_id``, which the firmware mints per print, so a
|
||||
leftover ``status="printing"`` row from a completion we never saw cannot
|
||||
hand this print someone else's objects. Without one, nothing is loaded
|
||||
rather than guessed — the reload path on ``GET /print/objects`` covers that
|
||||
case on demand.
|
||||
"""
|
||||
client = printer_manager.get_client(printer_id)
|
||||
if client is None or client.state.printable_objects:
|
||||
return
|
||||
|
||||
subtask_id = str(getattr(state, "subtask_id", "") or "").strip()
|
||||
if subtask_id in ("", "0"):
|
||||
return
|
||||
|
||||
from backend.app.models.archive import PrintArchive
|
||||
|
||||
archive = await db.scalar(
|
||||
select(PrintArchive)
|
||||
.where(
|
||||
PrintArchive.printer_id == printer_id,
|
||||
PrintArchive.status == "printing",
|
||||
PrintArchive.subtask_id == subtask_id,
|
||||
)
|
||||
.order_by(PrintArchive.created_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
if archive is not None:
|
||||
_load_objects_from_archive(archive, printer_id, logger)
|
||||
|
||||
|
||||
async def on_print_start(printer_id: int, data: dict):
|
||||
"""Handle print start - archive the 3MF file immediately."""
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -4798,6 +4837,7 @@ async def on_print_running_observed(printer_id: int, data: dict):
|
||||
logger.info("[RESTART] Restored active Bambuddy print for printer %s", printer_id)
|
||||
|
||||
await _restore_usage_tracking_session(printer_id, state, db, logger)
|
||||
await _restore_printable_objects(printer_id, state, db, logger)
|
||||
|
||||
result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
||||
printer = result.scalar_one_or_none()
|
||||
|
||||
Reference in New Issue
Block a user