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