cw-orchestrator
cw-orchestrator copied to clipboard
Fix undefined behavior on simultaneous write in daemon state file
Simultaneous daemon state file writes currently are not handled. This PR aims to handle such cases. So there are 2 scenarios when simultaneous write can happen:
- Another program that uses daemon state in the process of writing in this file
- Another daemon of this program tries to write into a file
How this PR tries to fix it: For the 1.
- We lock a file with
file-lock
, which ensures another program won't be able to write in this file (We don't lock read-only daemonstates)
For the 2.
- We create global state of locked files by this program with
once_cell::sync::Lazy
. - We disallow other daemons from any thread to access this lock, unless it's cloned directly from the daemon, who originally locked it.
- As soon as every daemon dropped state we unlock file
Beyond safety this PR adds few optimizations:
- Write json to a file only when all of the state clones done with using this state file
- Read from a shared state instead of file, when it's in a locked state