Skip to content
12 changes: 8 additions & 4 deletions cortex/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import subprocess
import threading
from datetime import datetime
from pathlib import Path
from typing import Any, ClassVar
Expand Down Expand Up @@ -54,6 +55,7 @@ def __init__(self, sandbox_executor=None):
self.sandbox_executor = sandbox_executor
self.cortex_dir = Path.home() / ".cortex"
self.preferences_file = self.cortex_dir / "preferences.yaml"
self._file_lock = threading.Lock() # Protect file I/O operations

# Ensure .cortex directory exists with secure permissions
self.cortex_dir.mkdir(mode=0o700, exist_ok=True)
Expand Down Expand Up @@ -280,8 +282,9 @@ def _load_preferences(self) -> dict[str, Any]:
"""
if self.preferences_file.exists():
try:
with open(self.preferences_file) as f:
return yaml.safe_load(f) or {}
with self._file_lock:
with open(self.preferences_file) as f:
return yaml.safe_load(f) or {}
except Exception:
pass

Expand All @@ -295,8 +298,9 @@ def _save_preferences(self, preferences: dict[str, Any]) -> None:
preferences: Dictionary of preferences to save
"""
try:
with open(self.preferences_file, "w") as f:
yaml.safe_dump(preferences, f, default_flow_style=False)
with self._file_lock:
with open(self.preferences_file, "w") as f:
yaml.safe_dump(preferences, f, default_flow_style=False)
except Exception as e:
raise RuntimeError(f"Failed to save preferences: {e}")

Expand Down
Loading
Loading