fix: close multipart file handles after mime-type validation - #1085
Open
stareezy-1 wants to merge 1 commit into
Open
fix: close multipart file handles after mime-type validation#1085stareezy-1 wants to merge 1 commit into
stareezy-1 wants to merge 1 commit into
Conversation
MimeTypeValidator.Validate opened the uploaded file to sniff its content type but never closed the handle. For parts stored on disk (above the adapter memory threshold) each call leaked a file descriptor until the GC finalizer ran, and readFile leaked its own handle on the validation-error path. Close both handles so upload bursts do not exhaust the descriptor limit.
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
Fixes #1077
Problem
MimeTypeValidator.Validateopens the uploaded file to sniff its content type and never closes it. For any part above the adapter multipart memory threshold (8 KiB forhumago), that handle is a real file on disk, so the descriptor stays held until the GC finalizer runs.readFilealso opens a handle before validation and returns without closing it when validation fails.multipart.Form.RemoveAll()only unlinks the temp file; it does not close handles returned byFileHeader.Open(). A service doing a lot of uploads can drift on file descriptors, and bursts can exhaust the descriptor limit before the finalizer runs.Reproduction: upload a file larger than the adapter memory threshold with a mime type that fails validation in a loop; the process accumulates two open file descriptors per request.
Fix
Validate:defer file.Close()immediately after the open check, covering the success, sniff-error, and invalid-mime-type paths.readFile: close its handle before returning the validation error (the success path transfers ownership to the caller).Tests
Two regression tests added to
formdata_internal_test.go, both driving a disk-backed part with the GC disabled andRLIMIT_NOFILElowered to 80 so leaked descriptors are observable:TestMimeTypeValidatorClosesFileEMFILEafter ~50 iterations)TestReadFileClosesOnValidationErrorFailed to open fileAll existing tests continue to pass (
go test -race ./...).