fdleak

package
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package fdleak provides file descriptor leak detection.

It takes a snapshot of open file descriptors before and after running the tested function.

Any file descriptors present in the "after" snapshot but not in the "before" snapshot — and not of a filtered Kind — are considered leaks.

Platform support

  • Linux: enumerates /proc/self/fd and classifies FDs from the readlink target (socket:/pipe:/anon_inode:/ path).
  • darwin: enumerates /dev/fd and resolves each FD via fcntl(F_GETPATH), falling back to fstat to classify sockets, pipes and kqueues.
  • other: Snapshot returns an error.

Filtering

Sockets, pipes and other kernel-internal descriptors (Linux anon_inode, darwin kqueue) are excluded from leak reports by default, as these are typically managed by the Go runtime or external libraries.

Concurrency

This approach is inherently process-wide: the FD table lists all file descriptors for the process.

Any concurrent I/O from other goroutines may cause false positives. A mutex serializes Leaked calls to prevent multiple leak checks from interfering with each other, but cannot protect against external concurrent file operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatLeaked

func FormatLeaked(leaked []FDInfo) string

FormatLeaked formats leaked file descriptors into a human-readable message. Returns the empty string if the slice is empty.

func Leaked

func Leaked(tested func()) (string, error)

Leaked takes a before/after snapshot around the tested function and returns a formatted description of leaked file descriptors.

Returns the empty string if no leaks are found. On unsupported platforms, Leaked returns an error.

func Snapshot

func Snapshot() (map[int]FDInfo, error)

Snapshot returns a map of currently open file descriptors for the running process.

The set of supported platforms is determined at build time; see the per-platform implementations (fdleak_linux.go, fdleak_darwin.go).

On unsupported platforms, Snapshot returns an error.

FDs that close between enumeration and resolution are silently skipped.

Types

type FDInfo

type FDInfo struct {
	FD     int
	Kind   Kind
	Target string
}

FDInfo describes an open file descriptor.

It should remain a human-readable description: a file-system path for vnode-backed FDs, or a synthetic label such as "socket:[<inode>]" for non-vnode kinds.

Kind is the authoritative classification; use it rather than parsing Target when filtering.

func Diff

func Diff(before, after map[int]FDInfo) []FDInfo

Diff returns file descriptors present in after but not in before, excluding filtered FD kinds (sockets, pipes, other kernel descriptors).

type Kind added in v2.5.0

type Kind int

Kind classifies an open file descriptor independently of platform-specific target-string conventions.

const (
	// KindUnknown is used when the descriptor kind could not be determined.
	KindUnknown Kind = iota

	// KindFile denotes anything backed by a path in the file system:
	// regular files, directories, symlinks, block devices.
	KindFile

	// KindSocket denotes a socket (AF_UNIX, AF_INET, …).
	KindSocket

	// KindPipe denotes a pipe or FIFO.
	KindPipe

	// KindChar denotes a character device without a resolvable path
	// (fallback for darwin when F_GETPATH fails on a char device).
	KindChar

	// KindOther covers kernel-level descriptors that are not directly
	// opened by user code: Linux anon_inode (epoll, timerfd, …), darwin
	// kqueue, and similar.
	KindOther
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL