Skip to content

Commit

Permalink
fix: add mutex against possible race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
foosel committed Nov 12, 2024
1 parent e45d740 commit 5cce729
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions octoprint_file_check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self):

self._native_grep_available = False

self._last_check_mutex = threading.RLock()
self._full_check_lock = threading.RLock()
self._check_result = {}

Expand Down Expand Up @@ -278,12 +279,13 @@ def _save_last_check_info(self):
}

try:
with open(
os.path.join(self.get_plugin_data_folder(), "last_check_info.json"),
"w",
encoding="utf-8",
) as f:
data = json.dump(data, f)
with self._last_check_mutex:
with open(
os.path.join(self.get_plugin_data_folder(), "last_check_info.json"),
"w",
encoding="utf-8",
) as f:
data = json.dump(data, f)
except Exception:
self._logger.exception(
"Could not save information about last full file check"
Expand All @@ -302,13 +304,18 @@ def _load_last_check_info(self):
return {}

try:
with open(
path,
encoding="utf-8",
) as f:
data = json.load(f)
if isinstance(data, dict) and "version" in data and "timestamp" in data:
return data
with self._last_check_mutex:
with open(
path,
encoding="utf-8",
) as f:
data = json.load(f)
if (
isinstance(data, dict)
and "version" in data
and "timestamp" in data
):
return data
except Exception:
self._logger.exception(
"Could not load information about last full file check"
Expand Down

0 comments on commit 5cce729

Please sign in to comment.