git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Worktree creation race

From
MCMarketa Calabkova <mcalabkova@suse.cz>
Date
Jan 15, 2019, 14:03 UTC
Message-ID
<89985b60-4c28-ebf0-64b1-8da75263d745@suse.cz>
Hello,
I am writing to report a bug. The original report is from my colleague, I am also providing his suggestions.
There is insufficient locking for worktree addition. Adding worktree may fail.

The problem is that git reads the directory entries in $GIT_DIR/worktrees, finds a worktree name that does not exist, tries to create it, and if an error is returned adding the worktree fails. When multiple git processes do this in parallel only one adds a worktree and the others fail. Git should reread the directory and find a new name that does not exist when creating the worktree directory fails because another git process already created it.

I suppose adding PID in the tree name would mitigate the issue to the point it will be very unlikely to encounter.
I need more than the tree in the temporary directory so using the temporary directory directly as a tree is out of question.
to test:

cd /dev/shm mkdir gittest cd gittest git init gitrepo cd gitrepo git commit --allow-empty -m Empty for n in $(seq 10000) ; do ( tmp=$(mktemp -d /dev/shm/gittest/test.XXXXXXXXXXX) ; mkdir $tmp/test ; git worktree add --detach $tmp/test ; ) & done

(you should see many messages like: fatal: could not create directory of '.git/worktrees/test284': File exists)

Greetings, Marketa

Next: Marketa Calabkova
Message 1 of 7 in “Worktree creation race”
  1. Marketa CalabkovaJan 15, 2019
  2. Marketa CalabkovaJan 28, 2019
  3. Eric SunshineFeb 1, 2019
  4. Duy NguyenFeb 1, 2019
  5. Duy NguyenFeb 1, 2019
  6. Eric SunshineFeb 1, 2019
  7. Junio C HamanoFeb 1, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.