Skip to content

Close and delete multipart upload tempfiles once the response is closed - #3086

Open
ericproulx wants to merge 1 commit into
masterfrom
fix/tempfile-reaper-around-router
Open

ericproulx wants to merge 1 commit into
masterfrom
fix/tempfile-reaper-around-router

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

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.

  • Around the router, not each endpoint. The reaper wraps the router in API::Instance#initialize, next to Rack::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 with mount, which goes through no endpoint's stack. Both cases have a spec, and both specs fail with the reaper in build_stack.
  • On by default, as dblock concluded on Add Rack::TempfileReaper to the endpoint middleware stack #3041. A Grape API inside Rails gets a second reaper, and closing a closed tempfile does nothing. A spec runs the API under an outer Rack::TempfileReaper.
  • An unrescued exception also reaps the files. On Add Rack::TempfileReaper to the endpoint middleware stack #3041 I noted Rack 2.2 didn't do this; Grape now requires Rack 3.1, where Rack::TempfileReaper does it too.

Why not Rack::TempfileReaper itself

Wrapping 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.tempfiles Array and a BodyProxy for every request, and the BodyProxy forwards each through method_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). So Grape::Middleware::TempfileReaper looks 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::TempfileReaper would have raised FrozenError on. 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:

  • a 201;
  • an error! 422;
  • an exception that escapes the API;
  • a route that cascades to another that reads the same upload;
  • a mounted Rack app that answers a frozen triplet;
  • an outer 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 rake passes.

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

@ericproulx
ericproulx force-pushed the fix/tempfile-reaper-around-router branch from 98ea28d to bbd182e Compare November 20, 2026 22:36
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Danger Report

No issues found.

View run

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
ericproulx force-pushed the fix/tempfile-reaper-around-router branch from bbd182e to 42ba85a Compare November 21, 2026 10:02
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.

Include Rack::TempFileReaper in Grape ?

2 participants