'Connection reset by peer' when replying before the end of POST data

Lunar lunar at anargeek.net
Fri Mar 2 16:03:53 UTC 2012


Eric Wong wrote:
> Lunar <lunar at anargeek.net> wrote:
> > With your proposed patch, everything is working as intended. At least, I
> > can confirm Firefox correctly display the error message sent by the
> > server and not the less understandable "Connection reset by peer". I can
> > also confirm that it does not send the complete file: it looks like it
> > stops sending as soon as either it notices the socket is closed on its
> > write part, or when the response arrives. I am satisfied enough not to
> > dive in libxpcom…
> 
> Cool, thanks for confirming.  It's good that your client knows to back
> off.

Actually, they don't, I was a little too quick to answer. I made some
more test, sending a 8.1 MB file over a Wi-Fi connection between two
systems. For each test, I created a pcap file by recording the HTTP
transmission.

curl is 7.21.2-4, and I am using:

    curl -v -F "file=@test-file.xcf;type=application/x-xcf" \
            -F "submit=submit" http://webserver/

epiphany is 2.30.6-1, using WebKit and IIRC, libsoup for its HTTP
connections. Iceweasel is version 3.5.16-12. w3m is 0.5.2-10. All coming
from Debian Squeeze.

Here's what's hapenning for nginx (0.7.67-3+squeeze1) when
`client_max_body` is set to `1m`:

  - curl: 413 displayed, 9.5K
  - w3m: 413 displayed, 8.8M
  - iceweasel: 413 displayed, 8.8M
  - epiphany: 413 displayed, 8.8M

For Apache (2.2.16-6+squeeze6), using `LimitRequestBody 1048576`:

  - curl: 413 displayed, 1.8K
  - w3m: 413 displayed, 8.8M
  - iceweasel: 413 displayed, 8.9M
  - epiphany: 413 displayed, 8.8M

So it looks usual HTTP web server and clients will drain all the POST
input in order to display the error message. *sigh*


Then I set up Rainbows! with the following (running latest gems on Ruby
1.8.7):

--- 8< --- rainbows.conf -----------------------------------------------
Rainbows! do
  use :ThreadSpawn
  rewindable_input false
end
--- >8 -----------------------------------------------------------------

--- 8< --- rackup.ru ---------------------------------------------------
run lambda do |env|
  while env['rack.input'].read; end;
  [ 200, { 'Content-Type' => 'text/plain' }, [ 'OK' ] ]
end
--- >8 -----------------------------------------------------------------

Same tests with this configuration:

  - curl: OK displayed, 9.5M
  - w3m: OK displayed, 9.5M
  - iceweasel: OK displayed, 9.5M
  - epiphany: OK displayed, 11M

If I add `client_max_body_size 1024 * 1024` in rainbows.conf:

  - curl: 413 displayed, 1.4K
  - w3m: 413 displayed, 11K
  - iceweasel: connection reset by peer, 21K
  - epiphany: connection terminated unexpectedly, 20K

> My original patch isn't safe for bad clients that send continously.
> Below is a safer version of my original patch, can you see if it
> works?
> 
> diff --git a/lib/rainbows/client.rb b/lib/rainbows/client.rb
> index b456eca..1dcb6d4 100644
> --- a/lib/rainbows/client.rb
> +++ b/lib/rainbows/client.rb
> @@ -6,4 +6,16 @@ class Rainbows::Client < Kgio::Socket
>    include Rainbows::ProcessClient
>  
>    alias write kgio_write
> +
> +  def close
> +    close_write
> +    kgio_wait_readable(2)
> +    buf = ""
> +    case kgio_tryread(512, buf)
> +    when nil, Symbol
> +      break
> +    end while true
> +    ensure
> +      super
> +  end
>  end

Using this patch (actually, defining Rainbows::Client#close in
rackup.ru), I get:

  - curl: 413 displayed, 1.5K
  - w3m: 413 displayed, 17K
  - iceweasel: connection reset by peer, 19K
  - epiphany: connection terminated unexpectedly, 16K

I have tried to come up with something closer to what other webservers
do, and it looks like it (but I might have overlooked something):

  def close
    close_write
    buf = ""
    loop do
      kgio_wait_readable(2)
      break unless kgio_tryread(512, buf)
    end
    ensure
      super
  end

Using this, I get the following results:

  - curl: 413 displayed, 1.4K
  - w3m: 413 displayed, 8.8M
  - iceweasel: 413 displayed, 8.8M
  - epiphany: 413 displayed, 8.8M


This whole issue leaves me severely disappointed. It looks like I either
have to waste much of my users time (by forcing them to upload the whole
file) or leave them clueless if the file is too big (by cutting the
connection early, resulting in a "reset by peer message"). What a
choice…

Hope that helps,
-- 
Lunar


More information about the rainbows-talk mailing list