-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net, os: net.Conn.File.Fd should return a blocking descriptor
Historically net.Conn.File.Fd has returned a descriptor in blocking mode. That was broken by CL 495079, which changed the behavior for os.OpenFile and os.NewFile without intending to affect net.Conn.File.Fd. Use a hidden os entry point to preserve the historical behavior, to ensure backward compatibility. Change-Id: I8d14b9296070ddd52bb8940cb88c6a8b2dc28c27 Reviewed-on: https://go-review.googlesource.com/c/go/+/496080 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
- Loading branch information
1 parent
d694046
commit b950cc8
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2023 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. | ||
|
||
//go:build unix | ||
|
||
package net | ||
|
||
import ( | ||
"internal/syscall/unix" | ||
"testing" | ||
) | ||
|
||
// For backward compatibility, opening a net.Conn, turning it into an os.File, | ||
// and calling the Fd method should return a blocking descriptor. | ||
func TestFileFdBlocks(t *testing.T) { | ||
ls := newLocalServer(t, "unix") | ||
defer ls.teardown() | ||
|
||
errc := make(chan error, 1) | ||
done := make(chan bool) | ||
handler := func(ls *localServer, ln Listener) { | ||
server, err := ln.Accept() | ||
errc <- err | ||
if err != nil { | ||
return | ||
} | ||
defer server.Close() | ||
<-done | ||
} | ||
if err := ls.buildup(handler); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer close(done) | ||
|
||
client, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer client.Close() | ||
|
||
if err := <-errc; err != nil { | ||
t.Fatalf("server error: %v", err) | ||
} | ||
|
||
// The socket should be non-blocking. | ||
rawconn, err := client.(*UnixConn).SyscallConn() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = rawconn.Control(func(fd uintptr) { | ||
nonblock, err := unix.IsNonblock(int(fd)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !nonblock { | ||
t.Fatal("unix socket is in blocking mode") | ||
} | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
file, err := client.(*UnixConn).File() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// At this point the descriptor should still be non-blocking. | ||
rawconn, err = file.SyscallConn() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = rawconn.Control(func(fd uintptr) { | ||
nonblock, err := unix.IsNonblock(int(fd)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !nonblock { | ||
t.Fatal("unix socket as os.File is in blocking mode") | ||
} | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fd := file.Fd() | ||
|
||
// Calling Fd should have put the descriptor into blocking mode. | ||
nonblock, err := unix.IsNonblock(int(fd)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if nonblock { | ||
t.Error("unix socket through os.File.Fd is non-blocking") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters