From 60039518e03b0f1a0f530eefe008ebf72c55afe4 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jul 2010 10:10:06 +0000 Subject: ensure file response bodies are properly closed Middlewares like Clogger may wrap Rack::File responses with another body that responds to to_path and still rely on #close to trigger an action (writing out the log file). --- lib/rainbows/event_machine.rb | 5 +++- lib/rainbows/rev/client.rb | 3 ++- lib/rainbows/rev/sendfile.rb | 12 +--------- lib/rainbows/stream_file.rb | 5 ++-- t/file-wrap-to_path.ru | 24 +++++++++++++++++++ t/t0021-sendfile-wrap-to_path.sh | 51 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 t/file-wrap-to_path.ru create mode 100644 t/t0021-sendfile-wrap-to_path.sh diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb index 4faa7a6..696f5a0 100644 --- a/lib/rainbows/event_machine.rb +++ b/lib/rainbows/event_machine.rb @@ -121,7 +121,10 @@ module Rainbows if st.file? write(response_header(status, headers)) if headers @body = stream = stream_file_data(body.to_path) - stream.callback { quit } unless alive + stream.callback do + body.close if body.respond_to?(:close) + quit unless alive + end return elsif st.socket? || st.pipe? chunk = stream_response_headers(status, headers) if headers diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb index 4d1aaec..502615e 100644 --- a/lib/rainbows/rev/client.rb +++ b/lib/rainbows/rev/client.rb @@ -8,6 +8,7 @@ module Rainbows include Rainbows::ByteSlice include Rainbows::EvCore G = Rainbows::G + F = Rainbows::StreamFile def initialize(io) CONN[self] = false @@ -80,7 +81,7 @@ module Rainbows if st.file? write(response_header(status, headers)) if headers - return defer_body(to_sendfile(io)) + return defer_body(F.new(0, io, body)) elsif st.socket? || st.pipe? return stream_response(status, headers, io, body) end diff --git a/lib/rainbows/rev/sendfile.rb b/lib/rainbows/rev/sendfile.rb index 414cfa1..9f421f1 100644 --- a/lib/rainbows/rev/sendfile.rb +++ b/lib/rainbows/rev/sendfile.rb @@ -2,12 +2,6 @@ # :enddoc: module Rainbows::Rev::Sendfile if IO.method_defined?(:sendfile_nonblock) - F = Rainbows::StreamFile - - def to_sendfile(io) - F[0, io] - end - def rev_sendfile(body) body.offset += @_io.sendfile_nonblock(body, body.offset, 0x10000) enable_write_watcher @@ -15,12 +9,8 @@ module Rainbows::Rev::Sendfile enable_write_watcher end else - def to_sendfile(io) - io - end - def rev_sendfile(body) - write(body.sysread(0x4000)) + write(body.to_io.sysread(0x4000)) end end end diff --git a/lib/rainbows/stream_file.rb b/lib/rainbows/stream_file.rb index f533f18..dec58b0 100644 --- a/lib/rainbows/stream_file.rb +++ b/lib/rainbows/stream_file.rb @@ -5,10 +5,11 @@ # models. We always maintain our own file offsets in userspace because # because sendfile() implementations offer pread()-like idempotency for # concurrency (multiple clients can read the same underlying file handle). -class Rainbows::StreamFile < Struct.new(:offset, :to_io) +class Rainbows::StreamFile < Struct.new(:offset, :to_io, :body) def close - to_io.close + body.close if body.respond_to?(:close) + to_io.close unless to_io.closed? self.to_io = nil end end diff --git a/t/file-wrap-to_path.ru b/t/file-wrap-to_path.ru new file mode 100644 index 0000000..f12e08d --- /dev/null +++ b/t/file-wrap-to_path.ru @@ -0,0 +1,24 @@ +# must be run without Rack::Lint since that clobbers to_path +class Wrapper < Struct.new(:app) + def call(env) + status, headers, body = app.call(env) + body = Body.new(body) if body.respond_to?(:to_path) + [ status, headers, body ] + end + + class Body < Struct.new(:body) + def to_path + body.to_path + end + + def each(&block) + body.each(&block) + end + + def close + ::File.open(ENV['fifo'], 'wb') { |fp| fp.puts "CLOSING" } + end + end +end +use Wrapper +run Rack::File.new(Dir.pwd) diff --git a/t/t0021-sendfile-wrap-to_path.sh b/t/t0021-sendfile-wrap-to_path.sh new file mode 100644 index 0000000..4ae5929 --- /dev/null +++ b/t/t0021-sendfile-wrap-to_path.sh @@ -0,0 +1,51 @@ +#!/bin/sh +. ./test-lib.sh +test -r random_blob || die "random_blob required, run with 'make $0'" +case $RUBY_ENGINE in +ruby) ;; +*) + t_info "skipping $T since it can't load the sendfile gem, yet" + exit 0 + ;; +esac + +t_plan 7 "sendfile wrap body response for $model" + +t_begin "setup and startup" && { + rtmpfiles out err + rainbows_setup $model + echo 'require "sendfile"' >> $unicorn_config + echo 'def (::IO).copy_stream(*x); abort "NO"; end' >> $unicorn_config + + # can't load Rack::Lint here since it clobbers body#to_path + export fifo + rainbows -E none -D file-wrap-to_path.ru -c $unicorn_config + rainbows_wait_start +} + +t_begin "read random blob sha1" && { + random_blob_sha1=$(rsha1 < random_blob) +} + +t_begin "start FIFO reader" && { + cat $fifo > $out & +} + +t_begin "single request matches" && { + sha1=$(curl -sSfv 2> $err http://$listen/random_blob | rsha1) + test -n "$sha1" + test x"$sha1" = x"$random_blob_sha1" +} + +t_begin "body.close called" && { + wait # for cat $fifo + grep CLOSING $out || die "body.close not logged" +} + +t_begin "shutdown server" && { + kill -QUIT $rainbows_pid +} + +t_begin "check stderr" && check_stderr + +t_done -- cgit v1.2.3-24-ge0c7