Skip to content

[bug] Potential resource leak due to unclosed listener in mcp.go #1935

@docker-agent

Description

@docker-agent

🟠 high - bug

File: cmd/root/mcp.go (line 48)

Code

ln, err := listenAndCloseOnCancel(ctx, f.listenAddr)
if err != nil {
	return err
}

return mcp.StartHTTPServer(ctx, agentFilename, f.agentName, &f.runConfig, ln)

Problem

The ln listener is created by listenAndCloseOnCancel and passed to mcp.StartHTTPServer. While listenAndCloseOnCancel spawns a goroutine to close the listener when the context is cancelled, if mcp.StartHTTPServer returns an error before the context is cancelled, or if it doesn't explicitly close the listener, the listener may remain open, causing a resource leak.

Suggested Fix

Add a defer ln.Close() right after the listener is successfully created to ensure it's always closed when the function exits, regardless of how mcp.StartHTTPServer exits.


Found by nightly codebase scan

Metadata

Metadata

Assignees

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