1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00

tests: Fix "text file busy" race condition in go-build-cached

There are a couple of places in the tests when we attempt to build and
run simultaneously. Here, if the build is slow, there is a race where
"text file busy" can appear.

To fix this, build to a temporary file with a random name, then
atomically rename it to the final binary name.
This commit is contained in:
Alberto Bertogli
2025-10-18 15:21:29 +00:00
parent 08273ea901
commit 7a4a4e4b34

View File

@@ -53,8 +53,21 @@ function go-build-cached() { (
! cmp -s .flags-new .flags >/dev/null 2>&1 ||
[ "$(basename "$PWD")" -ot ".reference" ] ;
then
# Build to a temporary file first, then atomically rename it.
# This prevents "text file busy" errors when the binary is
# executed while being written to.
BINARY="$(basename "$PWD")"
TMPFILE=".${BINARY}.tmp.${RANDOM}"
# shellcheck disable=SC2086
go build -tags="$GOTAGS" $GOFLAGS
if go build -tags="$GOTAGS" $GOFLAGS -o "$TMPFILE"; then
# Atomically replace the binary.
mv "$TMPFILE" "$BINARY"
else
# Clean up the temp file if the build failed.
rm -f "$TMPFILE"
exit 1
fi
# Write to .flags instead of renaming, to prevent races where
# was .flags-new is already renamed by the time we get here.