Conversation
initInputReader only cancels the reader it replaces and never closes it, so every ReleaseTerminal/RestoreTerminal cycle (Exec, suspend) leaks the reader's resources. On Linux that is one epoll file descriptor per cycle. Close the old reader once its read loop has exited. If waitForReadLoop timed out the loop may still be using it, so it is left alone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CONTRIBUTING.md.What
initInputReaderreplacesp.cancelReaderwithout closing the previous one.ReleaseTerminalonly callsCancel()on it, andRestoreTerminalthen creates a new reader over it. The old reader's resources are never released.On Linux the cancel reader is epoll-based (
muesli/cancelreader), so eachReleaseTerminal/RestoreTerminalcycle (everytea.Exec, every suspend/resume) leaks one epoll file descriptor. The reader's cancel pipe is an*os.Fileand eventually gets reclaimed by its finalizer. The raw epoll fd is a plainintand never does.We hit this in a long-running TUI that execs into
tmux attachwhenever the user opens a session. After 9 days the process held 896 open fds, 856 of themanon_inode:[eventpoll]. The same code is in v1.3.10.Fix
Close the reader that is being replaced, but only once its read loop has exited (
readLoopDoneis closed). IfwaitForReadLooptimed out, the loop may still be reading from the old reader, so it is left alone, as before.shutdownstill closes the current reader as it does today, so there is no double close.Before / after
tty_linux_test.goruns 20ReleaseTerminal/RestoreTerminalcycles on a program reading from anos.Pipeand countsanon_inode:[eventpoll]entries in/proc/self/fd.Before:
After: passes (also with
-race -count=20).Also checked:
go test -race ./...,golangci-lintv2.9 (0 issues),go vetfor linux/windows/darwin/freebsd, and the examples module builds.