two daylog -a invocations can race and corrupt data.
problem
Append reads the file, appends in memory, then writes back. if two processes write at the same time:
- process a reads file
- process b reads file
- process a writes (a + content)
- process b writes (a + different content) — overwrites a's entry
reproduction
for i in {1..10}; do daylog -a "entry $i" & done
wait
daylog show # some entries missing
fix
use os.OpenFile with O_APPEND flag instead of read-modify-write:
f, err := os.OpenFile(d.Path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(content)
atomic append means the kernel handles the ordering. no locking needed.
two
daylog -ainvocations can race and corrupt data.problem
Appendreads the file, appends in memory, then writes back. if two processes write at the same time:reproduction
fix
use
os.OpenFilewithO_APPENDflag instead of read-modify-write:atomic append means the kernel handles the ordering. no locking needed.