Skip to content

Prevent Descriptor Leak - #132

Open
thomersch wants to merge 1 commit into
flopp:masterfrom
thomersch:descriptor-leak
Open

thomersch wants to merge 1 commit into
flopp:masterfrom
thomersch:descriptor-leak

Conversation

@thomersch

Copy link
Copy Markdown

While running go-staticmap as a library I noticed that descriptors were piling up, sometimes leading to resource exhaustion. It turned out that the tile fetcher doesn't close the http response body when the response is non-200 (e.g. a 404 or 500 response still has a body that needs closing). I moved defer resp.Body.Close() up, so it runs regardless of status code, and added tests to verify it's being closed. I've been running the patched version for a couple of days and the descriptor count went down massively.

Thanks for this great library!

TileFetcher.download deferred closing the HTTP response body only
after the status-code switch, so any non-200 response (404, 5xx,
etc.) returned early without closing the body, leaking the
underlying connection's file descriptor. Move the defer right after
the request succeeds so the body is always closed.

Add regression tests covering the 404 and 500 paths.
@thomersch thomersch changed the title Prevent File Descriptor Leak Prevent Descriptor Leak Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant