Skip to content

Are win32File objects truly closed on garbage collection?  #308

@JackMordaunt

Description

@JackMordaunt

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions