Repository navigation
fix(config): serialize config reads and writes with FileLock - #2184
aloneangel45 wants to merge 4 commits into
Conversation
|
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e3b6535515
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| os.replace(temp_path, file_path) | ||
| finally: | ||
| temp_path.unlink(missing_ok=True) | ||
| with _config_write_lock(file_path): |
There was a problem hiding this comment.
Lock before building caller snapshots
When separate processes perform read-modify-write updates such as ConfigManager.add_project(), both can load and edit the same old config before reaching this lock. It serializes the replacements, but the later writer still writes a stale full snapshot and can drop the earlier update; update_config() is not used by those mutation paths. Hold the lock across their reload, mutation, and write as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in the updated commit: ConfigManager.add_project, remove_project, and set_default_project now run reload -> check -> mutate -> save via _mutate_config_locked inside _config_write_lock so concurrent processes cannot overwrite each other's updates with stale snapshots. Added test_add_project_serializes_concurrent_processes and signed off for DCO. Verified with ruff format --check, ruff check, and pytest --noconftest -v tests/test_config_locking.py tests/test_config_save_reports_failure.py tests/cli/test_auto_update.py (45 passed).
e3b6535 to
e0f8915
Compare
Signed-off-by: aloneangel45 <aloneangel45@users.noreply.github.com>
|
Thanks for tackling #2181 — the re-entrant
|
…lock config CLI/promo Signed-off-by: aloneangel45 <aloneangel45@users.noreply.github.com>
|
@Flynn-Kawolsky Thank you for the detailed review! Updated the PR in
Verification on Windows after final edits: |
|
Thanks for the quick follow-up in Two concrete edge cases I found while reviewing and testing
Once those two are addressed and the account owner completes |
|
Updated in
Verification run locally on Windows (Python 3.14.3; optional modules estcontainers, dateparser,
|
181bc4d to
5edd898
Compare
… in CLI Signed-off-by: aloneangel45 <aloneangel45@users.noreply.github.com>
|
Thanks for turning around both fixes so quickly in One quick CI blocker I caught when running
def _retry_windows_permission_error(operation: Callable[[], _T]) -> _T:
"""Retry transient Windows sharing-violation PermissionError across concurrent readers/writers."""
if os.name != "nt":
return operation()
delay = 0.002
for _ in range(7):
try:
return operation()
except PermissionError:
time.sleep(delay)
delay = min(delay * 2, 0.05)
return operation()Once that one-liner is in (and the account owner completes |
Signed-off-by: aloneangel45 <aloneangel45@users.noreply.github.com>
|
Updated |
|
Thanks for the quick fix in
Once |
Summary
Serializes read-modify-replace operations on
config.jsonacross threads and concurrent MCP/CLI processes usingfilelock.FileLock.Root Cause
save_basic_memory_config()readconfig.jsonin_file_config_dict(), wrote a temporary file, and calledos.replace(temp_path, file_path)without holding an inter-process lock. Concurrent processes could overwrite each other's config changes or hitPermissionErrorduring concurrentos.replace()calls on Windows.Additionally,
_save_last_checked_timestamp()performed an unlockedload_config() -> mutate -> save_config()cycle that could overwrite concurrent changes written between load and save.Changes
_config_write_lockcontext manager insrc/basic_memory/config.pypairing a per-paththreading.RLock(plus re-entrancy guard within the same call stack) withfilelock.FileLock(bounded byCONFIG_LOCK_TIMEOUT_SECONDS = 10.0) around directory creation, read/merge, temp write, andos.replace.ConfigManager.update_config(mutate, *, persist_env_keys=...)to atomically reload, mutate, and writeconfig.jsonunder the same lock._save_last_checked_timestamp()insrc/basic_memory/cli/auto_update.pyto useConfigManager.update_configwhen available while keeping compatibility with lightweight test stubs (StubConfigManager).tests/test_config_locking.pycovering concurrent multi-process updates, lock-timeout error propagation, and nested initial config creation insideupdate_config.Verification
ruff format --check src/basic_memory/config.py src/basic_memory/cli/auto_update.py tests/test_config_locking.py: passedruff check src/basic_memory/config.py src/basic_memory/cli/auto_update.py tests/test_config_locking.py: passedpytest --noconftest -v tests/test_config_locking.py tests/test_config_save_reports_failure.py tests/cli/test_auto_update.py:44 passed(--noconftestused locally becausetests/conftest.pyimports full DB/embeddings dependencies not installed in this isolated environment)Fixes #2181