Skip to content

Commit 9923f44

Browse files
authored
Merge pull request #64 from systemetric/lock-image
Lock image before sending to clients to ensure image integrity
2 parents 9db7c3e + fac5037 commit 9923f44

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

shepherd/blueprints/run/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
import sys
9+
from fcntl import lockf, LOCK_EX, LOCK_UN
910
from enum import Enum
1011

1112
from flask import Blueprint, redirect, url_for, request, session, send_file
@@ -51,7 +52,14 @@ def toggle_auto_refresh():
5152

5253
@blueprint.route("/picture")
5354
def get_picture():
54-
return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg")
55+
try:
56+
f = os.open("/home/pi/shepherd/shepherd/static/image.jpg", os.O_RDWR)
57+
lockf(f, LOCK_EX)
58+
return send_file("/home/pi/shepherd/shepherd/static/image.jpg", mimetype="image/jpeg")
59+
finally:
60+
if "f" in locals():
61+
lockf(f, LOCK_UN)
62+
os.close(f)
5563

5664
@blueprint.route("/start", methods=["POST"])
5765
def start():

0 commit comments

Comments
 (0)