# WEBrick causing ruby process to hang? (on windows)

**URL:** <https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697>\
**Category:** ruby-talk\
**Created:** [20 September 2004 20:44 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697 "2004-09-20T20:44:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_McConnell1](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@Kevin\_McConnell1](https://rubytalk.org/u/Kevin_McConnell1)\
**Post date:** [20 September 2004 20:44 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/1 "2004-09-20T20:44:39Z")

</div>

When I run a ruby script that uses webrick, it seems to hang on exit.  
I'm using ruby 1.8.2 on Windows XP. (I don't remember having this  
sort of problem using linux, but I don't have a machine here to verify  
that.)

For example, if I run the following:

require 'webrick'

server = WEBrick::HTTPServer.new(  
&nbsp;&nbsp;:Port =\> 8081,  
&nbsp;&nbsp;:DocumentRoot =\> '.'  
)

['INT', 'TERM'].each {|signal| trap(signal) {  
&nbsp;&nbsp;&nbsp;&nbsp;puts "got #{signal}, shutting down..."  
&nbsp;&nbsp;&nbsp;&nbsp;server.shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;puts "OK"  
&nbsp;&nbsp;}}  
server.start

This will start up fine, but on hitting CTRL-C it'll display both  
'puts', and then hang. I have to kill the process with task manager.

Any thoughts as to what I'm doing wrong?

Cheers,  
&nbsp;&nbsp;Kevin

---

<div class="post-metadata">

**Author:** ![Lennon\_Day-Reynolds1](https://avatars.discourse-cdn.com/v4/letter/l/5f8ce5/32.png) [@Lennon\_Day-Reynolds1](https://rubytalk.org/u/Lennon_Day-Reynolds1)\
**Post date:** [20 September 2004 20:59 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/2 "2004-09-20T20:59:50Z")

</div>

I had a number of problems with this, which turned out to be related  
to keep-alive connections, not Windows signal-handling.

Try disabling keep-alive, (either in your HTML head section, or using  
raw headers in your servlets) and see if the process shuts down  
normally.

> **···**
>
> --  
> Lennon  
> [rcoder.net](http://rcoder.net)

---

<div class="post-metadata">

**Author:** ![Kevin\_McConnell1](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@Kevin\_McConnell1](https://rubytalk.org/u/Kevin_McConnell1)\
**Post date:** [20 September 2004 22:39 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/3 "2004-09-20T22:39:38Z")

</div>

Lennon Day-Reynolds wrote:

\> Try disabling keep-alive, (either in your HTML head section, or using  
\> raw headers in your servlets) and see if the process shuts down  
\> normally.

I'll give that a go, although interestingly it'll hang even if it has never served a single item. Simply starting the script and then trying to terminate it right away will make it hang.

---

<div class="post-metadata">

**Author:** ![Kevin\_McConnell1](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@Kevin\_McConnell1](https://rubytalk.org/u/Kevin_McConnell1)\
**Post date:** [21 September 2004 04:59 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/4 "2004-09-21T04:59:37Z")

</div>

Just to follow up... I checked on my home machines (one Linux, one Max  
OS X) and they were both fine using that same piece of sample code.  
So it definately just seems to be Windows. (Or perhaps it's just \*my\*  
Windows machine 🙂

It's annoying though, as it really slows down the edit/test cycle...

&nbsp;&nbsp;Kevin

---

<div class="post-metadata">

**Author:** ![Marcel\_Molina\_Jr](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Marcel\_Molina\_Jr](https://rubytalk.org/u/Marcel_Molina_Jr)\
**Post date:** [21 September 2004 14:42 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/5 "2004-09-21T14:42:19Z")

</div>

if the issue here is that you are shutting down webrick frequently because  
you are developing and want to test then you can feed :auto\_reload =\>  
true to webrick's option hash.

it will auto-reload all the classes on each request.

marcel

> **···**
>
> On Tue, Sep 21, 2004 at 01:59:37PM +0900, Kevin M wrote:
> 
> > Just to follow up... I checked on my home machines (one Linux, one Max  
> > OS X) and they were both fine using that same piece of sample code.  
> > So it definately just seems to be Windows. (Or perhaps it's just \*my\*  
> > Windows machine 🙂
> > 
> > It's annoying though, as it really slows down the edit/test cycle...
> 
> --  
> Marcel Molina Jr. \<marcel@vernix.org\>

---

<div class="post-metadata">

**Author:** ![Kevin\_McConnell1](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@Kevin\_McConnell1](https://rubytalk.org/u/Kevin_McConnell1)\
**Post date:** [21 September 2004 14:59 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/6 "2004-09-21T14:59:40Z")

</div>

Marcel Molina Jr. wrote:

> if the issue here is that you are shutting down webrick frequently because  
> you are developing and want to test then you can feed :auto\_reload =\>  
> true to webrick's option hash.

Cool, that sounds like exactly what I need to work around the problem.

Later, when I have some more time, I'll see if I can find the cause of the hanging.

Thanks for your help,  
&nbsp;&nbsp;&nbsp;Kevin

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [22 September 2004 20:00 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/7 "2004-09-22T20:00:46Z")

</div>

Kevin McConnell schrieb:

> Later, when I have some more time, I'll see if I can find the cause of the hanging.

Hi Kevin,

I found this while looking at some old code of mine:

&nbsp;&nbsp;&nbsp;trap("INT"){ s.shutdown; raise IOError }

With this I could shutdown WEBrick hitting CTRL-C in the DOS window. The raise was necessary, but I don't remember why.

HTH

Pit

---

<div class="post-metadata">

**Author:** ![Kevin\_McConnell1](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@Kevin\_McConnell1](https://rubytalk.org/u/Kevin_McConnell1)\
**Post date:** [22 September 2004 22:14 UTC](https://rubytalk.org/t/webrick-causing-ruby-process-to-hang-on-windows/13697/8 "2004-09-22T22:14:35Z")

</div>

Pit Capitain wrote:

> &nbsp;&nbsp;trap("INT"){ s.shutdown; raise IOError }

Cool, thanks, that does the trick. I guess WEBrick must leave a thread stuck blocking on a socket call, and the raise breaks it out of the block. Or something like that. Either way, this fixes it, which is good enough for me 🙂

Cheers,  
&nbsp;&nbsp;&nbsp;Kevin
