lin
2025-06-05 ed3dd9d3e7519a82bb871d5eedb24a2fa0c91f47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
//+build !nacl,!plan9,!windows,!js
 
package runtime_test
 
import (
   "internal/testenv"
   "io/ioutil"
   "os"
   "os/exec"
   "path/filepath"
   "syscall"
   "testing"
   "time"
)
 
// Issue #27250. Spurious wakeups to pthread_cond_timedwait_relative_np
// shouldn't cause semasleep to retry with the same timeout which would
// cause indefinite spinning.
func TestSpuriousWakeupsNeverHangSemasleep(t *testing.T) {
   testenv.MustHaveGoBuild(t)
   tempDir, err := ioutil.TempDir("", "issue-27250")
   if err != nil {
       t.Fatalf("Failed to create the temp directory: %v", err)
   }
   defer os.RemoveAll(tempDir)
 
   repro := `
    package main
 
    import "time"
 
    func main() {
        <-time.After(1 * time.Second)
    }
    `
   mainPath := filepath.Join(tempDir, "main.go")
   if err := ioutil.WriteFile(mainPath, []byte(repro), 0644); err != nil {
       t.Fatalf("Failed to create temp file for repro.go: %v", err)
   }
   binaryPath := filepath.Join(tempDir, "binary")
 
   // Build the binary so that we can send the signal to its PID.
   out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", binaryPath, mainPath).CombinedOutput()
   if err != nil {
       t.Fatalf("Failed to compile the binary: err: %v\nOutput: %s\n", err, out)
   }
   if err := os.Chmod(binaryPath, 0755); err != nil {
       t.Fatalf("Failed to chmod binary: %v", err)
   }
 
   // Now run the binary.
   cmd := exec.Command(binaryPath)
   if err := cmd.Start(); err != nil {
       t.Fatalf("Failed to start command: %v", err)
   }
   doneCh := make(chan error, 1)
   go func() {
       doneCh <- cmd.Wait()
   }()
 
   // With the repro running, we can continuously send to it
   // a non-terminal signal such as SIGIO, to spuriously
   // wakeup pthread_cond_timedwait_relative_np.
   unfixedTimer := time.NewTimer(2 * time.Second)
   for {
       select {
       case <-time.After(200 * time.Millisecond):
           // Send the pesky signal that toggles spinning
           // indefinitely if #27520 is not fixed.
           cmd.Process.Signal(syscall.SIGIO)
 
       case <-unfixedTimer.C:
           t.Error("Program failed to return on time and has to be killed, issue #27520 still exists")
           cmd.Process.Signal(syscall.SIGKILL)
           return
 
       case err := <-doneCh:
           if err != nil {
               t.Fatalf("The program returned but unfortunately with an error: %v", err)
           }
           return
       }
   }
}