fix: close HTTP response body explicitly instead of defer inside loop (resource leak)#35
Open
amathxbt wants to merge 1 commit into
Open
fix: close HTTP response body explicitly instead of defer inside loop (resource leak)#35amathxbt wants to merge 1 commit into
amathxbt wants to merge 1 commit into
Conversation
Using "defer resp.Body.Close()" inside the "for _, checkURI := range uris" loop means the bodies from all loop iterations are only closed when the outer Load() function returns, not at the end of each iteration. Under a long URI list this leaks open HTTP connections and file descriptors for the entire duration of the function. Fix: close the body immediately after io.ReadAll returns (and also in the DecodeResponse error branch) so each iteration cleans up after itself.
Author
|
Hey @akasuv @HappySean2845 @hughzhou-gif @ZhimaoL, flagging this one for the team. The defer inside the loop is easy to miss but it means connections are not being released between iterations. Most of the time it is fine but under any kind of network pressure or connection limit it will start causing hangs that are hard to trace back to this. Worth merging before it becomes a production issue. |
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.
Problem
In
cli/api.go, inside thefor _, checkURI := range urisloop, each iteration uses:deferin Go runs when the enclosing function returns, not when the loop iteration ends. With N URI candidates (service-desc, describedby, loader hints, entrypoint), N response bodies stay open simultaneously untilLoad()exits, holding N HTTP connections from the transport pool. Under connection limits or slow networks this causes pool exhaustion and hard-to-reproduce hangs.The
DecodeResponseerror branch also leaks — body is never closed on early return.Fix
defer resp.Body.Close()resp.Body.Close()immediately afterio.ReadAll(bytes fully consumed, safe to close)DecodeResponseerror branch to prevent leaking on early returnReferences