Skip to content

Commit

Permalink
heartbeat: lock ForkLock for reading
Browse files Browse the repository at this point in the history
Circumvents golang/go#22315
  • Loading branch information
nadiamoe committed Jul 9, 2024
1 parent e064a55 commit 11a582c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions testutil/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 11a582c

Please sign in to comment.