cw-orchestrator icon indicating copy to clipboard operation
cw-orchestrator copied to clipboard

Fix undefined behavior on simultaneous write in daemon state file

Open Buckram123 opened this issue 1 year ago • 14 comments

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:

  1. Another program that uses daemon state in the process of writing in this file
  2. 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

Buckram123 avatar Feb 12 '24 13:02 Buckram123