# Fast shutdown of webrick

**URL:** <https://rubytalk.org/t/fast-shutdown-of-webrick/14914>\
**Category:** ruby-talk\
**Created:** [22 November 2004 18:45 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914 "2004-11-22T18:45:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard1](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Simon\_Strandgaard1](https://rubytalk.org/u/Simon_Strandgaard1)\
**Post date:** [22 November 2004 18:45 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914/1 "2004-11-22T18:45:07Z")

</div>

On my box trap(:INT){ s.shutdown } takes some time to complete.  
Im wondering how one can shut it down fast and safe.

Is this an ok way to shut it down?

trap(:INT) do  
&nbsp;&nbsp;fork{ s.shutdown }  
&nbsp;&nbsp;exit!  
end

btw: Any clues how to quickly restart webrick ?

> **···**
>
> --  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![David\_Heinemeier\_Ha2](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@David\_Heinemeier\_Ha2](https://rubytalk.org/u/David_Heinemeier_Ha2)\
**Post date:** [22 November 2004 22:49 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914/2 "2004-11-22T22:49:14Z")

</div>

> Is this an ok way to shut it down?
> 
> btw: Any clues how to quickly restart webrick ?

I'm pretty interested in the answer to both of these questions :). The WEBrick servlet will be the preferred development platform from the next version of Rails (with really nice integrated breakpoint support and more!), so we're looking to make the experience as nice as possible.

(Yes, you can and should still deploy production apps on Apache)

> **···**
>
> --  
> David Heinemeier Hansson,  
> [http://www.basecamphq.com/](http://www.basecamphq.com/) -- Web-based Project Management  
> [http://www.rubyonrails.org/](http://www.rubyonrails.org/) -- Web-application framework for Ruby  
> [http://macromates.com/](http://macromates.com/) -- TextMate: Code and markup editor (OS X)  
> [http://www.loudthinking.com/](http://www.loudthinking.com/) -- Broadcasting Brain

---

<div class="post-metadata">

**Author:** ![Lloyd\_Zusman](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Lloyd\_Zusman](https://rubytalk.org/u/Lloyd_Zusman)\
**Post date:** [22 November 2004 23:01 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914/3 "2004-11-22T23:01:51Z")

</div>

Simon Strandgaard \<neoneye@adslhome.dk\> writes:

> On my box trap(:INT){ s.shutdown } takes some time to complete.  
> Im wondering how one can shut it down fast and safe.
> 
> Is this an ok way to shut it down?
> 
> trap(:INT) do  
> &nbsp;&nbsp;fork{ s.shutdown }  
> &nbsp;&nbsp;exit!  
> end

I'm not sure why you would need the fork.

I just do this, and it dies very quickly.

&nbsp;&nbsp;%w[SIGHUP SIGINT SIGQUIT SIGTERM].each {  
&nbsp;&nbsp;&nbsp;&nbsp;\>sig\>  
&nbsp;&nbsp;&nbsp;&nbsp;trap(sig) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# I forget whether or not the reference to the server  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# needs to be stored in a global in order for it to  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# be seen here. I do it anyway, just in case.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$s.shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}

> btw: Any clues how to quickly restart webrick ?

I use the 'runit' system to manage a lot of my daemons, including the  
one I use for webrick. Check it out here:

&nbsp;&nbsp;[http://smarden.org/runit](http://smarden.org/runit)

I can completely restart my webrick daemon in about 5-10 seconds using  
the 'runsvctrl' program that's part of this system.

I trap SIGHUP and SIGTERM above in addition to SIGINT because I can  
easily send these signals to the daemon via 'runsvctrl'. I also throw  
SIGQUIT in there because some systems switch the meaning of SIGQUIT and  
SIGINT, and I always forget which ones do which.

> **···**
>
> --  
> Lloyd Zusman  
> ljz@asfast.com  
> God bless you.

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck4](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@Jamis\_Buck4](https://rubytalk.org/u/Jamis_Buck4)\
**Post date:** [22 November 2004 22:59 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914/4 "2004-11-22T22:59:34Z")

</div>

David Heinemeier Hansson wrote:

> > Is this an ok way to shut it down?
> > 
> > btw: Any clues how to quickly restart webrick ?
> 
> I'm pretty interested in the answer to both of these questions :). The WEBrick servlet will be the preferred development platform from the next version of Rails (with really nice integrated breakpoint support and more!), so we're looking to make the experience as nice as possible.
> 
> (Yes, you can and should still deploy production apps on Apache)

I haven't done any exhaustive research into this topic, but this is what I've observed, along with my speculative conclusions:

1) When the last request to WEBrick results in an exception, I can shutdown WEBrick and it terminates quite snappily.

2) When the last request to WEBrick is successful (with no raised exception), WEBrick can take up to a minute to actually terminate.

I \*suspect\* that this has something to do with the browser keeping the connection to WEBrick alive. Perhaps WEBrick can't shut down as long as there is an active connection, and it has to wait for that connection to time out? Can anyone comment on how feasible this might be?

- Jamis

> **···**
>
> --  
> Jamis Buck  
> jgb3@email.byu.edu  
> [http://www.jamisbuck.org/jamis](http://www.jamisbuck.org/jamis)

---

<div class="post-metadata">

**Author:** ![GOTOU\_Yuuzou](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@GOTOU\_Yuuzou](https://rubytalk.org/u/GOTOU_Yuuzou)\
**Post date:** [23 November 2004 00:19 UTC](https://rubytalk.org/t/fast-shutdown-of-webrick/14914/5 "2004-11-23T00:19:23Z")

</div>

Hi,

In message \<41A26F33.4030303@email.byu.edu\>,

> **···**
>
> `Jamis Buck \<jgb3@email.byu.edu\>' wrote:
> 
> > I \*suspect\* that this has something to do with the browser keeping the  
> > connection to WEBrick alive. Perhaps WEBrick can't shut down as long as  
> > there is an active connection, and it has to wait for that connection to  
> > time out? Can anyone comment on how feasible this might be?
> 
> The attached patch adds a hook to wait till a request  
> arrives. But I don't know whether "io/wait" is usable on  
> every platform.
> 
> BTW, the keep-alive feature is disabled when the following  
> parameter is given.
> 
> &nbsp;&nbsp;&nbsp;:HTTPVersion =\> WEBrick::HTTPVersion.new("1.0")
> 
> --  
> gotoyuzo
> 
> # Index: lib/webrick/httpserver.rb
> 
> RCS file: /var/cvs/src/ruby/lib/webrick/httpserver.rb,v  
> retrieving revision 1.8  
> diff -u -p -F^[^A-Za-z0-9\_+-]\*\(class\|module\|def\)[^A-Za-z0-9\_+-] -r1.8 httpserver.rb  
> --- lib/webrick/httpserver.rb 21 Mar 2004 13:17:24 -0000 1.8  
> +++ lib/webrick/httpserver.rb 22 Nov 2004 23:47:05 -0000  
> @@ -15,6 +15,7 @@ require 'webrick/httprequest'  
> require 'webrick/httpresponse'  
> require 'webrick/httpservlet'  
> require 'webrick/accesslog'  
> +require 'io/wait'
> 
> module WEBrick  
> &nbsp;&nbsp;&nbsp;class HTTPServerError \< ServerError; end  
> @@ -46,6 +47,13 @@ def run(sock)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;req = HTTPRequest.new(@config)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server = self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
> + timeout = @config[:RequestTimeout]  
> + while timeout \> 0  
> + break if sock.wait(0.5)  
> + timeout = 0 if @status != :Running  
> + timeout -= 0.5  
> + end  
> + raise HTTPStatus::EOFError if timeout \<= 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;req.parse(sock)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.request\_method = req.request\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res.request\_uri = req.request\_uri
