aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Michel <victor@optimyze.cloud>2021-01-24 05:53:36 +0000
committerIan Lance Taylor <iant@golang.org>2021-01-26 00:30:18 +0000
commit1d5e14632edc2ba76156c8a771a2a1a5c5387326 (patch)
treecd1e7fa676577651a48e815f48e29379f694fc34
parentcf263e9f772eeeac21df6fe8f51a8c93e8ad5a27 (diff)
downloadgo-1d5e14632edc2ba76156c8a771a2a1a5c5387326.tar.gz
go-1d5e14632edc2ba76156c8a771a2a1a5c5387326.zip
os: further document limitations around naked file descriptors
NewFile requires the file descriptor to be either closed through the returned File instance, or to stay valid at least until the finalizer runs during garbage collection. These requirements are easily violated when file descriptors are closed via unix.Close, or when the *File returned by NewFile is garbage collected while the underlying file descriptor is still in use. This commit adds further documentation for NewFile and Fd, making it explicit that using naked file descriptors is subject to constraints due to garbage collection of File objects. Fixes #43863 Change-Id: I49ea1f0054eb2d2a72b616450c8e83476f4d07fb GitHub-Last-Rev: 180d0130ae9009456914fb265b4bafa0e599de0e GitHub-Pull-Request: golang/go#43867 Reviewed-on: https://go-review.googlesource.com/c/go/+/286032 Trust: Ian Lance Taylor <iant@golang.org> Reviewed-by: Rob Pike <r@golang.org>
-rw-r--r--src/os/file_unix.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/os/file_unix.go b/src/os/file_unix.go
index 0dc7a5a0a2..f88450018e 100644
--- a/src/os/file_unix.go
+++ b/src/os/file_unix.go
@@ -66,6 +66,10 @@ type file struct {
// making it invalid; see runtime.SetFinalizer for more information on when
// a finalizer might be run. On Unix systems this will cause the SetDeadline
// methods to stop working.
+// Because file descriptors can be reused, the returned file descriptor may
+// only be closed through the Close method of f, or by its finalizer during
+// garbage collection. Otherwise, during garbage collection the finalizer
+// may close an unrelated file descriptor with the same (reused) number.
//
// As an alternative, see the f.SyscallConn method.
func (f *File) Fd() uintptr {
@@ -90,6 +94,10 @@ func (f *File) Fd() uintptr {
// descriptor. On Unix systems, if the file descriptor is in
// non-blocking mode, NewFile will attempt to return a pollable File
// (one for which the SetDeadline methods work).
+//
+// After passing it to NewFile, fd may become invalid under the same
+// conditions described in the comments of the Fd method, and the same
+// constraints apply.
func NewFile(fd uintptr, name string) *File {
kind := kindNewFile
if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {