Conversation
|
Not included in this batch — main-side commit e9b5864 already added atomic writes + threading.RLock to pairing.py, creating heavy conflicts. Your unique addition (cross-process fcntl/msvcrt locking) is still valuable as a follow-up. Leaving open. |
|
Closing — main-side commit e9b5864 already added atomic writes (tempfile + os.replace) and threading.RLock protecting all read-modify-write operations in pairing.py. This handles the real-world concurrency case (multiple platform adapters in the same gateway process). Your unique addition — cross-process file locking via fcntl/msvcrt — would protect against CLI and gateway writing simultaneously, but that's a very narrow race window, and the atomic writes already prevent corruption (worst case is last-writer-wins, which is acceptable for pairing operations). The complexity of platform-specific locking doesn't justify the marginal improvement. Thanks for the thorough implementation @Dusk1e — the atomic write pattern and reentrant lock design were solid. |
Summary
This fixes a concurrency bug in the DM pairing store where concurrent gateway/CLI operations could silently overwrite each other's state.
Changes in this PR:
os.replace()Problem
gateway/pairing.pystored pairing state in JSON files, but the read/modify/write flow was not synchronized across processes.That meant concurrent operations like:
could race and cause silent state loss.
In practice, one writer could load stale JSON, modify it, and overwrite newer changes from another writer. This could drop:
Fix
This PR introduces a shared pairing store lock and wraps pairing state mutations in serialized transactions.
It also switches
_secure_write()to atomic persistence so state files are never left partially written if the process is interrupted mid-write.Tests
Added regression coverage for:
Files changed
gateway/pairing.pytests/gateway/test_pairing.pyManual verification
I attempted to run: