From 02a072734906ac4c1ea77990207b84895ab4a7cb Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 9 Jun 2015 20:17:18 +0000 Subject: ensure body is closed during hijack Middlewares such as Rack::Lock (used by Rails) break badly unless the response body is closed on hijack, so we will close it to follow the lead of other popular Rack servers. While it's unclear if there's anybody using rack.hijack with unicorn, we'll try to emulate the behavior of other servers as much as possible. ref: https://github.com/ngauthier/tubesock/issues/10 --- lib/unicorn/http_response.rb | 3 --- lib/unicorn/http_server.rb | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index 801bf9a..016dac8 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -52,12 +52,9 @@ module Unicorn::HttpResponse end if hijack - body = nil # ensure we do not close body hijack.call(socket) else body.each { |chunk| socket.write(chunk) } end - ensure - body.respond_to?(:close) and body.close end end diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb index 2657c29..9129ed8 100644 --- a/lib/unicorn/http_server.rb +++ b/lib/unicorn/http_server.rb @@ -559,16 +559,22 @@ class Unicorn::HttpServer # in 3 easy steps: read request, call app, write app response def process_client(client) status, headers, body = @app.call(env = @request.read(client)) - return if @request.hijacked? - if 100 == status.to_i - e100_response_write(client, env) - status, headers, body = @app.call(env) + begin return if @request.hijacked? + + if 100 == status.to_i + e100_response_write(client, env) + status, headers, body = @app.call(env) + return if @request.hijacked? + end + @request.headers? or headers = nil + http_response_write(client, status, headers, body, + @request.response_start_sent) + ensure + body.respond_to?(:close) and body.close end - @request.headers? or headers = nil - http_response_write(client, status, headers, body, - @request.response_start_sent) + unless client.closed? # rack.hijack may've close this for us client.shutdown # in case of fork() in Rack app client.close # flush and uncork socket immediately, no keepalive -- cgit v1.2.3-24-ge0c7