Commit 92f4c662 authored by Vladimir Malenovsky's avatar Vladimir Malenovsky
Browse files

add file_lock to prevent race condition due to truncate_signal()

parent bc5f0a1d
Loading
Loading
Loading
Loading
Loading
+19 −19
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Tuple, Optional
from os import path
import threading

import numpy as np
import pytest
@@ -62,6 +63,7 @@ from ..conftest import parse_properties
sys.path.append(SCRIPTS_DIR)
from pyaudio3dtools.audiofile import readfile, writefile

file_lock = threading.Lock()

def lc3plus_used(test_info, in_fmt, render_config):
    return (
@@ -157,6 +159,8 @@ def truncate_signal(
    Truncate the signal in in_file to maximum INPUT_DURATION_SEC seconds,
    and write the truncated signal to out_file
    """
    
    with file_lock:
        data, fs = readfile(in_file)

        if data.ndim == 1:
@@ -249,11 +253,9 @@ def run_full_chain_split_rendering(
            cut_in_file = output_path_base.joinpath(in_file.stem + "_cut" + in_file.suffix) 
            if not path.exists(cut_in_file):
                truncate_signal(in_file, cut_in_file)

            # !!!! debug only
            if not path.exists(cut_in_file):
               logging.error(f"Unable to cut file {str(in_file)} into {str(cut_in_file)}")
               pytest.fail(f"Unable to cut file {str(in_file)} into {str(cut_in_file)}")
            else:
                file_lock.acquire(timeout=10)  # wait max 10s until the lock is available
                file_lock.release()  # release immediately

            enc_cmd[3] = str(cut_in_file)
        else:
@@ -432,11 +434,9 @@ def run_external_split_rendering(
            cut_in_file = output_path_base.joinpath(in_file.stem + "_cut" + in_file.suffix) 
            if not path.exists(cut_in_file):
                truncate_signal(in_file, cut_in_file)

            # !!!! debug only
            if not path.exists(cut_in_file):
               logging.error(f"Unable to cut file {str(in_file)} into {str(cut_in_file)}")
               pytest.fail(f"Unable to cut file {str(in_file)} into {str(cut_in_file)}")
            else:
                file_lock.acquire(timeout=10)  # wait max 10s until the lock is available
                file_lock.release()  # release immediately

            split_pre_cmd[6] = str(cut_in_file)
        else: