Race Condition in File Creation | Generated by AI

Home 2026.04

Looking at the code path, a lock isn’t the root cause. The duplicate happens because create_filename silently falls back to a counter suffix when the slug already exists.

The race path the user hypothesizes (concurrent ww note):

Two invocations on the same machine can race past check_duplicate_notes():

  1. Both pass check_uncommitted_changes() and git pull --rebase
  2. Both call check_duplicate_notes() — neither sees the other’s file yet
  3. Both call create_filename() — one gets ...-en.md, the other hits the fallback at create_note_utils.py:77-79:

    while os.path.exists(file_path):
        file_path = os.path.join(notes_dir, f"{date_str}-{short_title}-{counter}-en.md")
    
  4. Both commit & push — end result: two near-identical notes.

A lock would prevent this race. But it wouldn’t prevent the more common path.

The likelier path (and what the test output actually shows):

The pair 2026-04-20-iphone-daylight-timelapse-en.md and ...-timelapse-1-en.md is the signature of the counter-suffix branch. That branch can be reached even sequentially, not just concurrently, when:

What actually fixes it:

  1. Make slug collision fatal in create_filename — if {date}-{slug}-en.md exists, abort (or require a flag). The counter-suffix fallback is the real hole.
  2. Strengthen check_duplicate_notes to also flag same-date same-slug before writing.
  3. Optionally add a flock on the notes dir for belt-and-suspenders against the in-process race.

Fix (1) is the highest-leverage — it closes both the race and the sequential repeat. Want me to make that change?


Generated by AI. Curating and sharing still takes effort. If you find it useful, feel free to donate. WeChat: @lzwjavaWeChat QR · X: @lzwjava · Say hi 👋

Back Donate