The function runtime.SetFinalizer(x, f interface{})
sets the finalizer associated with x
to f
.
What kind of objects are finalized by default?
What are some of the unintended pitfalls caused by having those objects finalized by default?
The function runtime.SetFinalizer(x, f interface{})
sets the finalizer associated with x
to f
.
What kind of objects are finalized by default?
What are some of the unintended pitfalls caused by having those objects finalized by default?
The following objects are finalized by default:
os.File
: The file is automatically closed when the object is garbage collected.
os.Process
: Finalization will release any resources associated with the process. On Unix, this is a no-operation. On Windows, it closes the handle associated with the process.
On Windows, it appears that package net
can automatically close a network connection.
The Go standard library is not setting a finalizer on object kinds other than the ones mentioned above.
There seems to be only one potential issue that may cause problems in actual programs: When an os.File
is finalized, it will make a call to the OS to close the file descriptor. In case the os.File
has been created by calling function os.NewFile(fd int, name string) *File
and the file descriptor is also used by another (different) os.File
, then garbage collecting either one of the file objects will render the other file object unusable. For example:
package main
import (
"fmt"
"os"
"runtime"
)
func open() {
os.NewFile(1, "stdout")
}
func main() {
open()
// Force finalization of unreachable objects
_ = make([]byte, 1e7)
runtime.GC()
_, err := fmt.Println("some text") // Print something via os.Stdout
if err != nil {
fmt.Fprintln(os.Stderr, "could not print the text")
}
}
prints:
could not print the text
some text
–
Nomism Just jump into the os.NewFile's source code:
// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
fdi := int(fd)
if fdi < 0 {
return nil
}
f := &File{&file{fd: fdi, name: name}}
runtime.SetFinalizer(f.file, (*file).close) // <<<<<<<<<<<<<<
return f
}
"What kind of objects are finalized by default?"
Nothing in Go is IMO finalized by default.
"What are some of the unintended pitfalls caused by having those objects finalized by default?"
As per above: none.
© 2022 - 2024 — McMap. All rights reserved.
runtime.SetFinalizer
documentation. You should never depend on or expect a finalizer to ever get run. Instead, always make sure to call the appropriate cleanup function(s) (e.g.defer file.Close()
right after checking the errors ofos.Open
). – Champaigne