From a9e428fb2307b9de553ae28596f57ea2dea7509f Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Fri, 4 Oct 2019 15:30:34 -0400 Subject: [PATCH] gvfs-helper: fix race condition when creating loose object dirs 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 --- gvfs-helper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gvfs-helper.c b/gvfs-helper.c index 6e9c0dfb707183..37726bfef99c21 100644 --- a/gvfs-helper.c +++ b/gvfs-helper.c @@ -1049,7 +1049,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);