Skip to content

race condition in append #77

Description

@notnmeyer

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:

  1. process a reads file
  2. process b reads file
  3. process a writes (a + content)
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions