From 9ccced22c38c30e776edc5c2513194623b42a7a9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 25 Mar 2009 16:25:45 -0700 Subject: Ensure Tempfiles are unlinked after every request Otherwise we bloat TMPDIR and run the host out of space, oops! --- lib/unicorn/http_request.rb | 1 + test/unit/test_upload.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index 411c56c..ee407ab 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -25,6 +25,7 @@ module Unicorn @parser.reset @params.clear @body.close rescue nil + @body.close! rescue nil @body = nil end diff --git a/test/unit/test_upload.rb b/test/unit/test_upload.rb index 41fc473..8246f65 100644 --- a/test/unit/test_upload.rb +++ b/test/unit/test_upload.rb @@ -50,6 +50,32 @@ class UploadTest < Test::Unit::TestCase assert_equal @sha1.hexdigest, resp[:sha1] end + def test_tempfile_unlinked + spew_path = lambda do |env| + if orig = env['HTTP_X_OLD_PATH'] + assert orig != env['rack.input'].path + end + assert_equal length, env['rack.input'].size + [ 200, @hdr.merge('X-Tempfile-Path' => env['rack.input'].path), [] ] + end + start_server(spew_path) + sock = TCPSocket.new(@addr, @port) + sock.syswrite("PUT / HTTP/1.0\r\nContent-Length: #{length}\r\n\r\n") + @count.times { sock.syswrite(' ' * @bs) } + path = sock.read[/^X-Tempfile-Path: (\S+)/, 1] + sock.close + + # send another request to ensure we hit the next request + sock = TCPSocket.new(@addr, @port) + sock.syswrite("PUT / HTTP/1.0\r\nX-Old-Path: #{path}\r\n" \ + "Content-Length: #{length}\r\n\r\n") + @count.times { sock.syswrite(' ' * @bs) } + path2 = sock.read[/^X-Tempfile-Path: (\S+)/, 1] + sock.close + assert path != path2 + + assert ! File.exist?(path) + end def test_put_keepalive_truncates_small_overwrite start_server(@sha1_app) -- cgit v1.2.3-24-ge0c7