unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: unicorn list <mongrel-unicorn@rubyforge.org>
Subject: Re: What happens when a client terminates a connection?
Date: Mon, 8 Aug 2011 14:47:29 -0700	[thread overview]
Message-ID: <20110808214729.GA4419@dcvr.yhbt.net> (raw)
In-Reply-To: <20110808211748.GB49840@jessebook-2.local>

Jesse Storimer <jstorimer@gmail.com> wrote:
> Eric Wong <normalperson@yhbt.net> wrote:
> > Unicorn has 4 distinct states :

<snip>

> > 3) inside Rack dispatch (after rack.input reading)
> >    Your app has no way of knowing your client disconnected at
> >    this stage.  You can hack Unicorn to IO.select in a separate
> >    thread, but there'll always be exposed windows leading up to
> >    4) so it's not worth it...
> > 
> > 4) writing the response: Unicorn will abort whenever a socket
> >    error is detected.  Keep in mind that every single part of the
> >    Rack response array can be dynamically generated by the app.
> >    Your application can still be "running" even though the Rack app
> >    has returned its response for Unicorn to start writing.
> > 
> >    Clients/Rack middleware can be written to detect this in the
> >    response body "close" method by checking if body.each completed.
> 
> Thanks for that explanation. Just so I understand, once the Rack application 
> enters 3) then it should be unaffected by a client disconnect, or any
> socket error? I'll definitely give PrereadInput a try in that case.

Yes, however I don't think I made it clear that 3) will /always/ transition
to state 4).  So you'll be able to use body.close to detect a client
write failure.

> When you say that Unicorn lazily reads request bodies, do you mean that
> my Rails application might already be in the middle of processing the
> request but Unicorn is still reading from the client socket?

Yes.  We use http://unicorn.bogomips.org/Unicorn::TeeInput.html by
default so it gives the Rack app a chance to reject a client
if it sees something it doesnt like

> At that point won't Rails have read all of the request body? Or does
> that only apply if I stick a middleware at the front of the stack that
> does all the heavy lifting?

I'm not certain about middlewares.  Rails may attempt to read all input
ASAP anyways depending on the request Content-Type/Encoding.  Check the
Rails/Rack sources for your versions of Rails/Rack to be sure.

-- 
Eric Wong
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying


  reply	other threads:[~2011-08-08 21:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20110808193252.GA7188@dcvr.yhbt.net>
2011-08-08 21:17 ` What happens when a client terminates a connection? Jesse Storimer
2011-08-08 21:47   ` Eric Wong [this message]
2011-08-08 16:19 Jesse Storimer
2011-08-08 19:28 ` Eric Wong
2011-08-08 20:13 ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110808214729.GA4419@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=mongrel-unicorn@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).