diff options
| author | Changkun Ou <hi@changkun.us> | 2020-09-24 08:57:00 +0200 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2020-09-27 02:23:55 +0000 |
| commit | 7bb6fed9b53494e9846689520b41b8e679bd121d (patch) | |
| tree | 261a8e4598134d06068832c7ad814ed832bed215 /src/runtime/mfinal.go | |
| parent | 6f02578f9cff92e6c0fae4d86df01dcf99673c61 (diff) | |
| download | go-7bb6fed9b53494e9846689520b41b8e679bd121d.tar.xz | |
os: document and emphasize a potential misuse of File.Fd
This CL revises the document of File.Fd that explicitly points
its user to runtime.SetFinalizer where contains the information
that a file descriptor could be closed in a finalizer and therefore
causes a failure in syscall.Write if runtime.KeepAlive is not invoked.
The CL also suggests an alternative of File.Fd towards File.SyscallConn.
Fixes #41505
Change-Id: I6816f0157add48b649bf1fb793cf19dcea6894b5
Reviewed-on: https://go-review.googlesource.com/c/go/+/256899
Reviewed-by: Rob Pike <r@golang.org>
Trust: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/mfinal.go')
| -rw-r--r-- | src/runtime/mfinal.go | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/runtime/mfinal.go b/src/runtime/mfinal.go index d6c85a8b93..cd6196dcab 100644 --- a/src/runtime/mfinal.go +++ b/src/runtime/mfinal.go @@ -293,15 +293,15 @@ func runfinq() { // pass the object to a call of the KeepAlive function to mark the // last point in the function where the object must be reachable. // -// For example, if p points to a struct that contains a file descriptor d, -// and p has a finalizer that closes that file descriptor, and if the last -// use of p in a function is a call to syscall.Write(p.d, buf, size), then -// p may be unreachable as soon as the program enters syscall.Write. The -// finalizer may run at that moment, closing p.d, causing syscall.Write -// to fail because it is writing to a closed file descriptor (or, worse, -// to an entirely different file descriptor opened by a different goroutine). -// To avoid this problem, call runtime.KeepAlive(p) after the call to -// syscall.Write. +// For example, if p points to a struct, such as os.File, that contains +// a file descriptor d, and p has a finalizer that closes that file +// descriptor, and if the last use of p in a function is a call to +// syscall.Write(p.d, buf, size), then p may be unreachable as soon as +// the program enters syscall.Write. The finalizer may run at that moment, +// closing p.d, causing syscall.Write to fail because it is writing to +// a closed file descriptor (or, worse, to an entirely different +// file descriptor opened by a different goroutine). To avoid this problem, +// call runtime.KeepAlive(p) after the call to syscall.Write. // // A single goroutine runs all finalizers for a program, sequentially. // If a finalizer must run for a long time, it should do so by starting |
