Conversation
net/http only RemoveAlls the original Request. Middleware that replaces c.Request via WithContext leaves /tmp/multipart-* from FormFile/MultipartForm orphaned. Clean c.Request at ServeHTTP end. Fixes gin-gonic#4278
staticcheck SA1029 forbids empty anonymous struct as context.WithValue keys; use a local named type instead.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4791 +/- ##
=======================================
Coverage 98.65% 98.65%
=======================================
Files 48 48
Lines 3190 3194 +4
=======================================
+ Hits 3147 3151 +4
Misses 43 43
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Context.cleanupMultipartForm()at the end ofEngine.ServeHTTPso temp files fromParseMultipartFormare removed even when middleware replacesc.Request(e.g. OTELWithContext).net/httponly runsMultipartForm.RemoveAll()on the original request handed toServeHTTP; a shallow-copied request leaves/tmp/multipart-*orphaned (Why no call to Request.MultipartForm.RemoveAll #4278).RemoveAllis safe when the request was not replaced (os.ErrNotExistignored). No public API change. Supersedes the approach in clean up the multipart request files- this resolves #4278 #4279 (cleanup must live in Gin's request lifecycle, not binding).Test plan
go test ./... -count=1 -run 'TestServeHTTPCleansMultipartForm|TestContextCleanupMultipartFormNilSafe'go test . -count=1WithContext,FormFilewith body >MaxMultipartMemory→ temp path gone afterServeHTTPcleanupMultipartFormnil-safe for nil Request / nil MultipartFormFixes #4278