microsoft / go-winio

Win32 IO-related utilities for Go
MIT License
950 stars 182 forks source link

Are win32File objects truly closed on garbage collection? #308

Open JackMordaunt opened 10 months ago

JackMordaunt commented 10 months ago

The doc comment for win32File indicates the file will be closed when the Go object is garbage collected.

// win32File implements Reader, Writer, and Closer on a Win32 handle without blocking in a syscall.
// It takes ownership of this handle and will close it if it is garbage collected.
type win32File struct {
    handle        windows.Handle
    wg            sync.WaitGroup
    wgLock        sync.RWMutex
    closing       atomicBool
    socket        bool
    readDeadline  deadlineHandler
    writeDeadline deadlineHandler
}

However, grepping through the codebase I cannot find a single call to runtime.SetFinalizer. I'm curious: if this doc comment is true, how is the file being closed on garbage collection without any such calls to runtime.SetFinalizer?

I'm writing some logic that serves net.Conn (provided by this package), and I want to know if I need to take care of setting a finalizer myself or not.

Thanks!