From e5faa0d34aaaa06ebbb3bbbc81d2398d0c24b012 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..d02a93f 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 from leaking + // into a forked process and thus making exec fail with ETXBSY. + // 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 {