I've been getting this strange error message in one of my build scripts – cp
fails, returning the error "File exists". I've even tried using cp -f
, which should overwrite the file if it exists, but the error still shows up. Running cp
to overwrite existing files works perfectly when I do it manually. What could cause this error?
Cannot Create Regular File ‘filename’: File Exists Error
concurrencycp
Best Answer
This turned out to be caused by a race condition.
cp
checks if the destination file already exists, and if not - overwrites it. The problem was happening because thiscp
command was being run twice in parallel, which caused the file in question to sometimes appear after checking whether it exists, but before the attempt to create the file. Thestrace
output looks like this:Here's some bash code used to catch this:
This same error can occur with
mkdir -p
or any other action that attempts to overwrite a file. Usingflock
can help avoid race conditions in cases like this.