From f5c93d4184a6ccd811e47d74475ec0b524a28e17 Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Sat, 15 Nov 2025 15:14:23 +0000 Subject: [PATCH 1/3] lock image file before sending to ensure full image is sent --- shepherd/blueprints/run/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/shepherd/blueprints/run/__init__.py b/shepherd/blueprints/run/__init__.py index 171af16..2228c26 100644 --- a/shepherd/blueprints/run/__init__.py +++ b/shepherd/blueprints/run/__init__.py @@ -6,6 +6,7 @@ import json import os import sys +from fcntl import lockf, LOCK_EX, LOCK_UN from enum import Enum from flask import Blueprint, redirect, url_for, request, session, send_file @@ -51,7 +52,13 @@ def toggle_auto_refresh(): @blueprint.route("/picture") def get_picture(): - return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg") + try: + f = os.open("/home/pi/shepherd/shepherd/static/image.jpg", os.O_RDWR) + lockf(f, LOCK_EX) + return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg") + finally: + lockf(f, LOCK_UN) + os.close(f) @blueprint.route("/start", methods=["POST"]) def start(): From f3381db01e273ced931a21950564cdc6946f263e Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Sat, 15 Nov 2025 15:17:23 +0000 Subject: [PATCH 2/3] ensure file is valid before releasing lock --- shepherd/blueprints/run/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shepherd/blueprints/run/__init__.py b/shepherd/blueprints/run/__init__.py index 2228c26..84f770a 100644 --- a/shepherd/blueprints/run/__init__.py +++ b/shepherd/blueprints/run/__init__.py @@ -57,8 +57,9 @@ def get_picture(): lockf(f, LOCK_EX) return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg") finally: - lockf(f, LOCK_UN) - os.close(f) + if f in locals(): + lockf(f, LOCK_UN) + os.close(f) @blueprint.route("/start", methods=["POST"]) def start(): From fac503706cd90972d0cb4c0f8e09a998327a50d3 Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Sat, 15 Nov 2025 15:26:25 +0000 Subject: [PATCH 3/3] fix syntax issue in `run/__init__.py` --- shepherd/blueprints/run/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shepherd/blueprints/run/__init__.py b/shepherd/blueprints/run/__init__.py index 84f770a..4e7f5d5 100644 --- a/shepherd/blueprints/run/__init__.py +++ b/shepherd/blueprints/run/__init__.py @@ -57,7 +57,7 @@ def get_picture(): lockf(f, LOCK_EX) return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg") finally: - if f in locals(): + if "f" in locals(): lockf(f, LOCK_UN) os.close(f)