From: Ian Lance Taylor Date: Tue, 6 Mar 2018 22:12:45 +0000 (-0800) Subject: internal/poll: if poller init fails, assume blocking mode X-Git-Tag: go1.11beta1~1320 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=558769a61b246464f979f3f4d370dad96cee2109;p=gostls13.git internal/poll: if poller init fails, assume blocking mode Fixes #23943 Change-Id: I16e604872f1615963925ec3c4710106bcce1330c Reviewed-on: https://go-review.googlesource.com/99015 Run-TryBot: Ian Lance Taylor Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/internal/poll/fd_unix.go b/src/internal/poll/fd_unix.go index 372dc81633..36376ef6cb 100644 --- a/src/internal/poll/fd_unix.go +++ b/src/internal/poll/fd_unix.go @@ -59,7 +59,13 @@ func (fd *FD) Init(net string, pollable bool) error { fd.isBlocking = true return nil } - return fd.pd.init(fd) + err := fd.pd.init(fd) + if err != nil { + // If we could not initialize the runtime poller, + // assume we are using blocking mode. + fd.isBlocking = true + } + return err } // Destroy closes the file descriptor. This is called when there are diff --git a/src/os/timeout_test.go b/src/os/timeout_test.go index 6f47ed04a9..6105f9b1a1 100644 --- a/src/os/timeout_test.go +++ b/src/os/timeout_test.go @@ -587,3 +587,36 @@ func TestRacyWrite(t *testing.T) { }() } } + +// Closing a TTY while reading from it should not hang. Issue 23943. +func TestTTYClose(t *testing.T) { + f, err := os.Open("/dev/tty") + if err != nil { + t.Skipf("skipping because opening /dev/tty failed: %v", err) + } + + go func() { + var buf [1]byte + f.Read(buf[:]) + }() + + // Give the goroutine a chance to enter the read. + // It doesn't matter much if it occasionally fails to do so, + // we won't be testing what we want to test but the test will pass. + time.Sleep(time.Millisecond) + + c := make(chan bool) + go func() { + defer close(c) + f.Close() + }() + + select { + case <-c: + case <-time.After(time.Second): + t.Error("timed out waiting for close") + } + + // On some systems the goroutines may now be hanging. + // There's not much we can do about that. +}