Skip to content

Commit 5cce729

Browse files
committed
fix: add mutex against possible race condition
1 parent e45d740 commit 5cce729

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

octoprint_file_check/__init__.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self):
4848

4949
self._native_grep_available = False
5050

51+
self._last_check_mutex = threading.RLock()
5152
self._full_check_lock = threading.RLock()
5253
self._check_result = {}
5354

@@ -278,12 +279,13 @@ def _save_last_check_info(self):
278279
}
279280

280281
try:
281-
with open(
282-
os.path.join(self.get_plugin_data_folder(), "last_check_info.json"),
283-
"w",
284-
encoding="utf-8",
285-
) as f:
286-
data = json.dump(data, f)
282+
with self._last_check_mutex:
283+
with open(
284+
os.path.join(self.get_plugin_data_folder(), "last_check_info.json"),
285+
"w",
286+
encoding="utf-8",
287+
) as f:
288+
data = json.dump(data, f)
287289
except Exception:
288290
self._logger.exception(
289291
"Could not save information about last full file check"
@@ -302,13 +304,18 @@ def _load_last_check_info(self):
302304
return {}
303305

304306
try:
305-
with open(
306-
path,
307-
encoding="utf-8",
308-
) as f:
309-
data = json.load(f)
310-
if isinstance(data, dict) and "version" in data and "timestamp" in data:
311-
return data
307+
with self._last_check_mutex:
308+
with open(
309+
path,
310+
encoding="utf-8",
311+
) as f:
312+
data = json.load(f)
313+
if (
314+
isinstance(data, dict)
315+
and "version" in data
316+
and "timestamp" in data
317+
):
318+
return data
312319
except Exception:
313320
self._logger.exception(
314321
"Could not load information about last full file check"

0 commit comments

Comments
 (0)