Source file src/os/timeout_unix_test.go

     1  // Copyright 2025 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 !js && !plan9 && !wasip1 && !windows
     6  
     7  package os_test
     8  
     9  import (
    10  	"os"
    11  	"os/signal"
    12  	"syscall"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func init() {
    18  	pipeDeadlinesTestCases = []pipeDeadlineTest{{
    19  		"anonymous pipe",
    20  		func(t *testing.T) (r, w *os.File) {
    21  			r, w, err := os.Pipe()
    22  			if err != nil {
    23  				t.Fatal(err)
    24  			}
    25  			return r, w
    26  		},
    27  	}}
    28  }
    29  
    30  // Closing a TTY while reading from it should not hang.  Issue 23943.
    31  func TestTTYClose(t *testing.T) {
    32  	// Ignore SIGTTIN in case we are running in the background.
    33  	signal.Ignore(syscall.SIGTTIN)
    34  	defer signal.Reset(syscall.SIGTTIN)
    35  
    36  	f, err := os.Open("/dev/tty")
    37  	if err != nil {
    38  		t.Skipf("skipping because opening /dev/tty failed: %v", err)
    39  	}
    40  
    41  	go func() {
    42  		var buf [1]byte
    43  		f.Read(buf[:])
    44  	}()
    45  
    46  	// Give the goroutine a chance to enter the read.
    47  	// It doesn't matter much if it occasionally fails to do so,
    48  	// we won't be testing what we want to test but the test will pass.
    49  	time.Sleep(time.Millisecond)
    50  
    51  	c := make(chan bool)
    52  	go func() {
    53  		defer close(c)
    54  		f.Close()
    55  	}()
    56  
    57  	select {
    58  	case <-c:
    59  	case <-time.After(time.Second):
    60  		t.Error("timed out waiting for close")
    61  	}
    62  
    63  	// On some systems the goroutines may now be hanging.
    64  	// There's not much we can do about that.
    65  }
    66  

View as plain text