From cc5685c2db1239775905f3911f0067c0fa74762f Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 24 Feb 2019 21:24:49 +0100 Subject: [PATCH] unix: don't overwrite unrelated file descriptors in TestDup TestDup used a file descriptor without ensuring it was free, leading to rare crashes in the runtime netpoller when the victim fd was the polling descriptor. Updates golang/go#29423 Change-Id: Idc8b6b47f7e966e045f57f2028e7b6b79e0fb3f3 Reviewed-on: https://go-review.googlesource.com/c/163638 Reviewed-by: Tobias Klauser --- unix/syscall_unix_test.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go index 7adc3a24..f6abe8c0 100644 --- a/unix/syscall_unix_test.go +++ b/unix/syscall_unix_test.go @@ -396,14 +396,24 @@ func TestDup(t *testing.T) { t.Fatalf("Dup: %v", err) } - err = unix.Dup2(newFd, newFd+1) + // Create and reserve a file descriptor. + // Dup2 automatically closes it before reusing it. + nullFile, err := os.Open("/dev/null") + if err != nil { + t.Fatal(err) + } + dupFd := int(file.Fd()) + err = unix.Dup2(newFd, dupFd) if err != nil { t.Fatalf("Dup2: %v", err) } + // Keep the dummy file open long enough to not be closed in + // its finalizer. + runtime.KeepAlive(nullFile) b1 := []byte("Test123") b2 := make([]byte, 7) - _, err = unix.Write(newFd+1, b1) + _, err = unix.Write(dupFd, b1) if err != nil { t.Fatalf("Write to dup2 fd failed: %v", err) }