fdleak

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package fdleak provides file descriptor leak detection.

It uses /proc/self/fd snapshots on Linux to take 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 are considered leaks.

By default, sockets, pipes, and anonymous inodes are filtered out, as these are typically managed by the Go runtime or OS internals.

This approach is inherently process-wide: /proc/self/fd 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.

This package only works on Linux. On other platforms, Snapshot returns an error.

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. The caller is responsible for checking runtime.GOOS before calling.

func Snapshot

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

Snapshot reads /proc/self/fd and returns a map of currently open file descriptors.

FDs that close between ReadDir and Readlink are silently skipped. Returns an error if not running on Linux.

Types

type FDInfo

type FDInfo struct {
	FD     int
	Target string // readlink target (e.g. "/tmp/foo.txt", "socket:[12345]")
}

FDInfo describes an open file descriptor.

func Diff

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

Diff returns file descriptors present in after but not in before, excluding filtered FD types (sockets, pipes, anonymous inodes).

Jump to

Keyboard shortcuts

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