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
roobre committed Jul 9, 2024
1 parent e064a55 commit e5faa0d
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 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 {
Expand Down

0 comments on commit e5faa0d

Please sign in to comment.