From d37cca52c9ccbe44070203a2b6e41868197531aa Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Fri, 9 Jul 2021 00:24:31 +1000 Subject: [PATCH] review: Avoid race conditions if called multiple times By always using unique filepaths (except for the final result, which is written atomically) we turn multiple simultanious calls into at worst wasted effort, instead of garbled results. --- restreamer/restreamer/review.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/restreamer/restreamer/review.py b/restreamer/restreamer/review.py index 638bd43..7da20c0 100644 --- a/restreamer/restreamer/review.py +++ b/restreamer/restreamer/review.py @@ -4,6 +4,7 @@ import logging import os import subprocess from urlparse import urlparse +from uuid import uuid4 import mysql.connector @@ -123,7 +124,8 @@ def review(match_id, race_number, base_dir, db_url, start_range=(0, 5), finish_r finish_paths = [] for racer_number, racer in enumerate((racer1, racer2)): - start_path = os.path.join(output_dir, "start-{}.mp4".format(racer_number)) + nonce = str(uuid4()) + start_path = os.path.join(output_dir, "start-{}-{}.mp4".format(racer_number, nonce)) logger.info("Cutting start for racer {} ({})".format(racer_number, racer)) start_start, start_end = add_range(start, start_range) @@ -159,12 +161,12 @@ def review(match_id, race_number, base_dir, db_url, start_range=(0, 5), finish_r # time since their actual start. finish_base = end + time_offset finish_start, finish_end = add_range(finish_base, finish_range) - finish_path = os.path.join(output_dir, "finish-{}.mp4".format(racer_number)) + finish_path = os.path.join(output_dir, "finish-{}-{}.mp4".format(racer_number, nonce)) finish_paths.append(finish_path) logger.info("Got time offset of {}, cutting finish at finish_base {}".format(time_offset, finish_base)) cut_to_file(logger, finish_path, base_dir, racer, finish_start, finish_end) - temp_path = "{}.tmp.mp4".format(result_path) + temp_path = "{}.{}.mp4".format(result_path, str(uuid4())) args = ['ffmpeg'] for path in finish_paths: args += ['-i', path]