From f21895e535e7e194e42dc0c6a6fe589dd2a8a698 Mon Sep 17 00:00:00 2001 From: Lyncoln Date: Sat, 24 Oct 2026 21:04:20 -0400 Subject: [PATCH] Extended buildSave to create a temp file and migrate that file to the live accessed file to prevent read/write race conditionns between threads --- core/runtime/savedata.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/core/runtime/savedata.py b/core/runtime/savedata.py index ea8913e..0b8075a 100644 --- a/core/runtime/savedata.py +++ b/core/runtime/savedata.py @@ -1,5 +1,7 @@ import json import sqlite3 +import tempfile +import os def buildSave(action, outputs): if hasattr(action, "savedata"): @@ -8,11 +10,34 @@ def buildSave(action, outputs): for save_name in action.savedata: save_data[save_name] = outputs[save_name] - with open(f"{action.savepath}", "w") as save_file: - json.dump(save_data, save_file) + #ith open(f"{action.savepath}", "w") as save_file: + # json.dump(save_data, save_file) - #print(f"Data saved by {action.name} to {action.savepath}") - return outputs + temporary_path = None + + #directory = os.path.dirname(os.path.abspath(path)) + directory = os.environ.get("TEMP_DIR") + try: + with tempfile.NamedTemporaryFile( + mode="w", + #dir=directory, + dir=directory, + delete=False + ) as file: + json.dump(save_data, file) + temporary_path = file.name + + os.replace(temporary_path, action.savepath) + + finally: + if temporary_path: + try: + os.remove(temporary_path) + except FileNotFoundError: + pass + + #print(f"Data saved by {action.name} to {action.savepath}") + return outputs def loadSave(action, outputs): if hasattr(action, "loaddata"):