Skip to content

Commit

Permalink
gvfs-helper: fix race condition when creating loose object dirs
Browse files Browse the repository at this point in the history
When two gvfs-helper processes are the first to create a loose object
directory, the processes (A and B in the timeline below) could have
the following race:

1. A sees that the directory does not exist.
2. B sees that the directory does not exist.
3. A creates the directory with success.
4. B fails to create the directory and fails.

Instead of having B fail here, just check for the directory's
existence before reporting an error. That solves the race and
allows tests to pass.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
  • Loading branch information
derrickstolee authored and dscho committed Aug 8, 2023
1 parent 3ecfd05 commit c2a077d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gvfs-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,9 @@ static void create_tempfile_for_loose(
strbuf_complete(&buf_path, '/');
strbuf_add(&buf_path, hex, 2);

if (!file_exists(buf_path.buf) && mkdir(buf_path.buf, 0777) == -1) {
if (!file_exists(buf_path.buf) &&
mkdir(buf_path.buf, 0777) == -1 &&
!file_exists(buf_path.buf)) {
strbuf_addf(&status->error_message,
"cannot create directory for loose object '%s'",
buf_path.buf);
Expand Down

0 comments on commit c2a077d

Please sign in to comment.