Concurrent project (de)registration can still lose an update: _persist_projects has no cross-process lock
Preconditions:
- I have made sure it's an actual issue, not a question (use GitHub Discussions instead).
- I have actually encountered this issue in practice. Issues that are based on (AI-based) code analysis only will be closed without comment.
- I have consulted the user guide and verified that the issue cannot be resolved by adjusting configuration/following recommended workflows.
- I have looked for similar issues and discussions, including closed ones.
Issue details:
- I have provided a meaningful title and description.
- I have explained how the issue arose and, where possible, added instructions on how to reproduce it.
- I have added details on my setup: Serena version, MCP client, OS, the programming language(s), relevant configuration adjustments and project specifics.
- If the issue relates to an application of Serena to an open-source project, I have added the link.
Summary
_persist_projects (src/serena/config/serena_config.py) reloads the on-disk config, merges it
with the caller's in-memory project list, and writes the result back, with no lock around that
sequence. Two Serena processes (the workflow the user guide recommends, one instance per project)
can still lose an update if their reload-merge-write windows overlap: whichever read happened
first writes back a stale merge and silently undoes the other one's change.
This is the residual half of #1850. That issue's own "Suggested implementation direction" asked
for an interprocess lock covering the reload, the merge and the save, and warned that locking only
the save is not enough since the list being written can already be stale. 8a4d116c (Fixes #1850)
added the merge but not the lock, so the window is still open, now also for
remove_registered_project/remove_project, added afterward.
Reproduction
Not timing-dependent. Two SerenaConfig instances load the same on-disk list [a, b]. One's
_persist_projects() (registering c) is paused on a background thread right after its own
reload, holding a stale [a, b] snapshot. The other's _persist_projects() (removing b) is then
run to completion: it reloads [a, b], removes b, writes [a]. Releasing the paused call lets it
resume with its stale snapshot: it adds c and writes [a, b, c]. b is back; the removal never
happened.
I have a fix (wraps the reload-merge-write in a filelock.FileLock, already a direct dependency
and already used this way in serena/jetbrains/launch_coordinator.py) with a regression test built
on the interleaving above, checked failing on current main and passing with the fix. Opening a
PR for it.
Environment
- Serena source commit
9fa423710732790de5052006b257afcac771d26e(current main), Python 3.11 - Reproduced with the project's own pytest suite; not tied to a particular MCP client
Source: oraios/serena