From 310677969f7ab4a5b2c31e49c44503e3f838a1bf Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 10 Aug 2023 09:25:44 -0700 Subject: [PATCH] Avoid race conditions with recursive rm (#50842) If two processes attempt to recursively delete a directory at the same time, then we can end up in a state where the initial `isdir` is `true`, but by the time it actually deletes the directory it is already gone. e.g. - https://buildkite.com/clima/climacore-ci/builds/2460#0189d254-76a9-474b-ad25-e5b16440d629/140-142 which is triggered by https://github.com/cjdoris/PackageExtensionCompat.jl/blob/636eb5a14ddf9134d004c93f598515903af26443/src/PackageExtensionCompat.jl#L59 - https://buildkite.com/clima/climacore-ci/builds/2457#0189c7fe-8872-40c5-9106-da2e621ff55a/139-150 which is triggered by https://github.com/JuliaGPU/GPUCompiler.jl/blob/06e670657d7ceebc1845d7c9534a8352c33490de/src/rtlib.jl#L152 I've been conservative and only applied this when `force=true`, but perhaps it should apply generally? (cherry picked from commit cbd3c89875afb0892c4e9e7559bca0254b2781d4) --- base/file.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/base/file.jl b/base/file.jl index 866e82b6e39c2..d6373c07e993a 100644 --- a/base/file.jl +++ b/base/file.jl @@ -303,7 +303,9 @@ function rm(path::AbstractString; force::Bool=false, recursive::Bool=false) try ret = ccall(:uv_fs_rmdir, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}), C_NULL, req, path, C_NULL) uv_fs_req_cleanup(req) - ret < 0 && uv_error("rm($(repr(path)))", ret) + if ret < 0 && !(force && ret == Base.UV_ENOENT) + uv_error("rm($(repr(path)))", ret) + end nothing finally Libc.free(req)