From 11a582cf0cf4389616eae4369ff20b9beec7f7b0 Mon Sep 17 00:00:00 2001 From: Ro Santalla Date: Tue, 9 Jul 2024 17:26:00 +0200 Subject: [PATCH] heartbeat: lock ForkLock for reading Circumvents https://github.com/golang/go/issues/22315 --- testutil/heartbeat.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/testutil/heartbeat.go b/testutil/heartbeat.go index 706c7e0..83668d3 100644 --- a/testutil/heartbeat.go +++ b/testutil/heartbeat.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strconv" "strings" + "syscall" "testing" "time" ) @@ -36,6 +37,12 @@ type Heartbeat struct { func NewHeartbeat(t *testing.T) Heartbeat { t.Helper() + // Lock ForkLock whenever we are writing to a file that will execute shortly after, to prevent its FD leaking into + // forked process and exec failing with text file busy. + // https://github.com/golang/go/issues/22315 + syscall.ForkLock.RLock() + defer syscall.ForkLock.RUnlock() + dir := t.TempDir() scriptFile, err := os.OpenFile(filepath.Join(dir, "heartbeat.sh"), os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.FileMode(0o700)) if err != nil {