Skip to content

[bug] Potential resource leak due to unclosed stdin in monitorStdin #1910

@docker-agent

Description

@docker-agent

🟡 medium - bug

File: cmd/root/api.go (line 90)

Code

go func() {
	select {
	case <-ctx.Done():
	case <-done:
	}
	stdin.Close()
}()

Problem

The stdin.Close() call inside the goroutine is intended to unblock the stdin.Read(buf) call. However, if the monitorStdin function returns (e.g., due to an error in stdin.Read) before the ctx.Done() or done channels are closed, the goroutine might remain active, holding onto the stdin file descriptor. This could lead to a resource leak if stdin is not closed by the main thread.

Suggested Fix

Ensure that stdin is always closed when the monitorStdin function returns, possibly by adding a defer stdin.Close() in the monitorStdin function itself, or by explicitly closing stdin in the main runAPICommand function after monitorStdin is no longer needed. The current defer close(done) only closes the channel, not the file.


Found by nightly codebase scan

Metadata

Metadata

Assignees

No one assigned

    Labels

    automatedIssues created by cagentkind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions