Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/exec/exec_linux_test.go

Documentation: os/exec

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

View as plain text