Close response body in SendEvent/HealthCheck to fix FD + memory leak (#15)#16
Open
michaeltoop wants to merge 1 commit into
Open
Close response body in SendEvent/HealthCheck to fix FD + memory leak (#15)#16michaeltoop wants to merge 1 commit into
michaeltoop wants to merge 1 commit into
Conversation
SendEvent discarded the http.Post response (`_, err := ...`) and never closed resp.Body. An unclosed/undrained body keeps the underlying TCP connection out of the keep-alive pool, leaking a connection + file descriptor on every call. SendEvent runs on every stats tick, so the agent's open-FD count and memory grow unbounded: long-lived agents hit "socket: too many open files" (event POSTs, then docker.sock stats fetches) and busy agents are OOM-killed (exit 137). Drain + close the body in SendEvent (and the matching http.Get in HealthCheck). Fixes swarmpit#15. Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.
Fixes #15.
Problem
SendEvent(swarmpit/client.go) discards thehttp.Postresponse and never closesresp.Body:An unclosed/undrained response body keeps the underlying TCP connection out of the keep-alive pool, so each call leaks a connection + file descriptor.
SendEventruns on every stats tick, so the agent's open-FD count and memory grow without bound.In production (7-node Swarm, agent as a
globalservice) this shows up as:socket: too many open files— first on the event POST (Post http://swarmpit:8080/events), then collaterally on the docker.sock stats fetches — after which the agent stays alive but stops shipping events (UI graphs go blank);exit 137,OOMKilled=true) on a roughly hourly cadence.The
ContainerUsagestats path already doesdefer resp.Body.Close(); onlySendEventwas missing it.Fix
Drain and close the body in
SendEvent(and the matchinghttp.GetinHealthCheck, which had the same omission):ioutil.Discardis used to stay compatible with the module'sgo 1.12(io.Discardlanded in 1.16). Builds clean withgo build ./swarmpit/.🤖 Generated with Claude Code