1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build linux && cgo
6
7// On systems that use glibc, calling malloc can create a new arena,
8// and creating a new arena can read /sys/devices/system/cpu/online.
9// If we are using cgo, we will call malloc when creating a new thread.
10// That can break TestExtraFiles if we create a new thread that creates
11// a new arena and opens the /sys file while we are checking for open
12// file descriptors. Work around the problem by creating threads up front.
13// See issue 25628.
14
15package exec_test
16
17import (
18	"os"
19	"sync"
20	"syscall"
21	"time"
22)
23
24func init() {
25	if os.Getenv("GO_EXEC_TEST_PID") == "" {
26		return
27	}
28
29	// Start some threads. 10 is arbitrary but intended to be enough
30	// to ensure that the code won't have to create any threads itself.
31	// In particular this should be more than the number of threads
32	// the garbage collector might create.
33	const threads = 10
34
35	var wg sync.WaitGroup
36	wg.Add(threads)
37	ts := syscall.NsecToTimespec((100 * time.Microsecond).Nanoseconds())
38	for i := 0; i < threads; i++ {
39		go func() {
40			defer wg.Done()
41			syscall.Nanosleep(&ts, nil)
42		}()
43	}
44	wg.Wait()
45}
46