Repository navigation
Close and delete multipart upload tempfiles once the response is closed - #3086
Open
ericproulx wants to merge 1 commit into
Open
ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
ericproulx
force-pushed
the
fix/tempfile-reaper-around-router
branch
from
November 20, 2026 22:36
98ea28d to
bbd182e
Compare
Danger ReportNo issues found. |
2 of 3 tasks
Rack's multipart parser leaves its tempfiles to whatever reaps them, and outside Rails nothing did, so a standalone Grape API kept every upload on disk until garbage collection or exit (#2487). A reaper now wraps the router, next to Rack::Head. There it also covers a mounted Rack app. Unlike one in each endpoint's stack, it doesn't fire when a route cascades and the router closes that route's body before the next route reads the same upload. Rack::TempfileReaper allocates an Array and a BodyProxy for every request, which cost 13-18% of a bare GET. Grape's reaper instead reads rack.tempfiles once the app has answered, and wraps the body only when the request uploaded something. Replaces #3041. Fixes #2487. Co-Authored-By: anyingiit <49945850+anyingiit@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix/tempfile-reaper-around-router
branch
from
November 21, 2026 10:02
bbd182e to
42ba85a
Compare
dblock
approved these changes
Nov 23, 2026
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
Rack's multipart parser creates a tempfile for each uploaded file and leaves it to whatever reaps it. Rails runs
Rack::TempfileReaper, but a Grape API served on its own did not, so every upload stayed on disk until garbage collection or the process exited (#2487). Grape now closes and deletes those tempfiles once the response is closed, or as soon as an exception escapes the API.This picks up #3041 by @anyingiit (credited as co-author) and addresses the review there.
API::Instance#initialize, next toRack::Head(Wrap the router in Rack::Head instead of each endpoint's stack #3042). In an endpoint's stack it would fire when a route cascades and the router closes that route's body, before the next route reads the same upload (IOError: closed stream). It would also miss a Rack app mounted withmount, which goes through no endpoint's stack. Both cases have a spec, and both specs fail with the reaper inbuild_stack.Rack::TempfileReaper.Rack::TempfileReaperdoes it too.Why not
Rack::TempfileReaperitselfWrapping the router with Rack's middleware made a bare GET 13–18% slower (5.0 → 5.8 µs in three A/B pairs) and added 5 allocations per request (11 → 16). It allocates the
rack.tempfilesArray and aBodyProxyfor every request, and theBodyProxyforwardseachthroughmethod_missing. Most requests upload nothing.Rack's multipart parser records the tempfiles it creates in
env['rack.tempfiles'](Rack 3.1 and 3.2 alike). SoGrape::Middleware::TempfileReaperlooks there once the app has answered, and wraps the body only when the request uploaded something. A request without an upload allocates 11 objects, as on master, and the three A/B pairs came out +2.5%, −0.3% and +1.1%.It also returns a new triplet instead of writing into the one it got: a mounted Rack app may answer a frozen one, which
Rack::TempfileReaperwould have raisedFrozenErroron. A spec covers that too.The one thing Rack's reaper covers and this one doesn't: a tempfile created after the response is returned, by a body that parses the upload while it streams. Such a file is left to garbage collection, as it was before this change.
Tests
spec/grape/integration/rack_spec.rb,describe 'multipart tempfiles'(#3041's two examples plus four), checking that the tempfile is gone after:error!422;Rack::TempfileReaper, as in Rails.The first five fail on master. All pass on Rack 3.2.8 and 3.1.23, and
bundle exec rakepasses.UPGRADING has an entry, since code that keeps an uploaded tempfile after the response now has to copy it first. The README says so too.
Replaces #3041. Fixes #2487.
🤖 Generated with Claude Code