Skip to content

fix(gin): close os.File in RunFd to prevent resource leak#4422

Merged
appleboy merged 1 commit intogin-gonic:masterfrom
1911860538:gin/runfd
Nov 7, 2025
Merged

fix(gin): close os.File in RunFd to prevent resource leak#4422
appleboy merged 1 commit intogin-gonic:masterfrom
1911860538:gin/runfd

Conversation

@1911860538
Copy link
Copy Markdown
Contributor

os.NewFile creates a *os.File object that holds a reference to
the file descriptor. Even though the file descriptor itself is
not opened by os.NewFile, the *os.File object needs to be cleaned
up to release its reference.

@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (3dc1cd6) to head (f600b0a).
⚠️ Report is 202 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4422      +/-   ##
==========================================
- Coverage   99.21%   98.96%   -0.25%     
==========================================
  Files          42       44       +2     
  Lines        3182     2900     -282     
==========================================
- Hits         3157     2870     -287     
- Misses         17       19       +2     
- Partials        8       11       +3     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.95% <100.00%> (?)
-tags go_json 98.88% <100.00%> (?)
-tags nomsgpack 98.94% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.24 98.96% <100.00%> (?)
go-1.25 98.96% <100.00%> (?)
macos-latest 98.96% <100.00%> (-0.25%) ⬇️
ubuntu-latest 98.96% <100.00%> (-0.25%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@appleboy appleboy changed the title fix: close os.File in RunFd to prevent resource leak fix(gin): close os.File in RunFd to prevent resource leak Nov 7, 2025
@appleboy appleboy added the type/bug Found something you weren't expecting? Report it here! label Nov 7, 2025
@appleboy appleboy added this to the v1.12 milestone Nov 7, 2025
@appleboy appleboy merged commit c3d5a28 into gin-gonic:master Nov 7, 2025
26 of 27 checks passed
@1911860538
Copy link
Copy Markdown
Contributor Author

Verified safe since net.FileListener internally duplicates the
file descriptor via dup(), creating a separate socket handle.
Closing the original *os.File only releases its own reference and
does not close the underlying network socket used by the listener.

Therefore, defer f.Close() does not interfere with existing connections
or cause "use of closed network connection" errors, including in
graceful restart scenarios where the listener is inherited across processes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/bug Found something you weren't expecting? Report it here!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants