Skip to content

Commit f2dbebb

Browse files
committed
Add Rack::TempfileReaper to the endpoint middleware stack
Multipart uploads parsed by Rack::Multipart create tempfiles that were left open on disk after the request unless the host app (e.g. Rails) already ran Rack::TempfileReaper. Use it in every endpoint's stack so they are closed and unlinked once the response body is closed. Fixes #2487
1 parent 0342ddc commit f2dbebb

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#3008](https://github.com/ruby-grape/grape/pull/3008): Add the grape-on-rack app as a live-server memory profiling harness - [@ericproulx](https://github.com/ericproulx).
1212
* [#3025](https://github.com/ruby-grape/grape/pull/3025): Add `:md` and `:markdown` formatters that render an object's `to_markdown`, for APIs that declare a `text/markdown` content type - [@ericproulx](https://github.com/ericproulx).
1313
* [#3029](https://github.com/ruby-grape/grape/pull/3029): Add a built-in `:jsonl` (JSON Lines) format that streams an Enumerator body - [@ericproulx](https://github.com/ericproulx).
14+
* [#3041](https://github.com/ruby-grape/grape/pull/3041): Add `Rack::TempfileReaper` to every endpoint's middleware stack so multipart upload tempfiles are unlinked after the response - [@anyingiit](https://github.com/anyingiit).
1415
* Your contribution here.
1516

1617
#### Fixes

‎lib/grape/endpoint.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def build_stack
462462
content_types = setting.content_types
463463
format = setting.format
464464

465+
stack.use Rack::TempfileReaper
465466
stack.use Rack::Head
466467
stack.use Grape::Middleware::Error, **error_middleware_options(format, content_types)
467468

‎spec/grape/integration/rack_spec.rb‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,46 @@
5050
end
5151
end
5252

53+
# https://github.com/ruby-grape/grape/issues/2487
54+
describe 'multipart tempfiles' do
55+
let(:uploaded_paths) { [] }
56+
57+
let(:app) do
58+
paths = uploaded_paths
59+
Class.new(Grape::API) do
60+
params do
61+
requires :file, type: File
62+
end
63+
64+
post 'upload' do
65+
paths << params[:file][:tempfile].path
66+
'ok'
67+
end
68+
69+
post 'failed_upload' do
70+
paths << params[:file][:tempfile].path
71+
error!('failed', 422)
72+
end
73+
end
74+
end
75+
76+
let(:upload) { Rack::Test::UploadedFile.new(__FILE__, 'text/plain') }
77+
78+
it 'unlinks the tempfile once the response is closed' do
79+
post '/upload', file: upload
80+
expect(last_response.body).to eq('ok')
81+
expect(uploaded_paths.size).to eq(1)
82+
expect(File.exist?(uploaded_paths.first)).to be(false)
83+
end
84+
85+
it 'unlinks the tempfile when the endpoint returns an error' do
86+
post '/failed_upload', file: upload
87+
expect(last_response.status).to eq(422)
88+
expect(uploaded_paths.size).to eq(1)
89+
expect(File.exist?(uploaded_paths.first)).to be(false)
90+
end
91+
end
92+
5393
context 'when the app is mounted' do
5494
let(:ping_mount) do
5595
Class.new(Grape::API) do

0 commit comments

Comments
 (0)