From ac05e7035e1946b78ce4679548db7680aa01734c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 17 Aug 2010 08:35:03 +0000 Subject: avoid EBADF with certain middlewares when proxying First off we use an FD_MAP to avoid creating redundant IO objects which map to the same FD. When that doesn't work, we'll fall back to trapping Errno::EBADF and IOError where appropriate. --- t/close-pipe-to_path-response.ru | 30 ++++++++++ t/t0032-close-pipe-to_path-response.sh | 101 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 t/close-pipe-to_path-response.ru create mode 100755 t/t0032-close-pipe-to_path-response.sh (limited to 't') diff --git a/t/close-pipe-to_path-response.ru b/t/close-pipe-to_path-response.ru new file mode 100644 index 0000000..abc3a37 --- /dev/null +++ b/t/close-pipe-to_path-response.ru @@ -0,0 +1,30 @@ +# must be run without Rack::Lint since that clobbers to_path +class MyMiddleware < Struct.new(:app) + class Body < Struct.new(:body, :to_path) + def each(&block); body.each(&block); end + def close + c = body.respond_to?(:close) + ::File.open(ENV['fifo'], 'wb') do |fp| + fp.syswrite("CLOSING #{body.inspect} #{to_path} (#{c})\n") + end + body.close if c + end + end + + def call(env) + status, headers, body = app.call(env) + body.respond_to?(:to_path) and body = Body.new(body, body.to_path) + [ status, headers, body ] + end +end +use MyMiddleware +use Rainbows::DevFdResponse +run(lambda { |env| + io = IO.popen('cat random_blob', 'rb') + [ 200, + { + 'Content-Length' => ::File.stat('random_blob').size.to_s, + 'Content-Type' => 'application/octet-stream', + }, + io ] +}) diff --git a/t/t0032-close-pipe-to_path-response.sh b/t/t0032-close-pipe-to_path-response.sh new file mode 100755 index 0000000..e3d8f1b --- /dev/null +++ b/t/t0032-close-pipe-to_path-response.sh @@ -0,0 +1,101 @@ +#!/bin/sh +. ./test-lib.sh +if ! test -d /dev/fd +then + t_info "skipping $T since /dev/fd is required" + exit 0 +fi + +t_plan 16 "close pipe to_path response for $model" + +t_begin "setup and startup" && { + rtmpfiles err out http_fifo sub_ok + rainbows_setup $model + export fifo + rainbows -E none -D close-pipe-to_path-response.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/ | 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 "start FIFO reader for abortive HTTP/1.1 request" && { + cat $fifo > $out & +} + +t_begin "send abortive HTTP/1.1 request" && { + rm -f $ok + ( + printf 'GET /random_blob HTTP/1.1\r\nHost: example.com\r\n\r\n' + dd bs=4096 count=1 < $http_fifo >/dev/null + echo ok > $ok + ) | socat - TCP:$listen > $http_fifo || : + test xok = x$(cat $ok) +} + +t_begin "body.close called for aborted HTTP/1.1 request" && { + wait # for cat $fifo + grep CLOSING $out || die "body.close not logged" +} + +t_begin "start FIFO reader for abortive HTTP/1.0 request" && { + cat $fifo > $out & +} + +t_begin "send abortive HTTP/1.0 request" && { + rm -f $ok + ( + printf 'GET /random_blob HTTP/1.0\r\n\r\n' + dd bs=4096 count=1 < $http_fifo >/dev/null + echo ok > $ok + ) | socat - TCP:$listen > $http_fifo || : + test xok = x$(cat $ok) +} + +t_begin "body.close called for aborted HTTP/1.0 request" && { + wait # for cat $fifo + grep CLOSING $out || die "body.close not logged" +} + +t_begin "start FIFO reader for abortive HTTP/0.9 request" && { + cat $fifo > $out & +} + +t_begin "send abortive HTTP/0.9 request" && { + rm -f $ok + ( + printf 'GET /random_blob\r\n' + dd bs=4096 count=1 < $http_fifo >/dev/null + echo ok > $ok + ) | socat - TCP:$listen > $http_fifo || : + test xok = x$(cat $ok) +} + +t_begin "body.close called for aborted HTTP/0.9 request" && { + 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