Niklas Hambüchen | 11 Apr 07:03 2013

Web servers: Running them multiple times in a ghci session

I'm writing a web server app, which I run in ghci:

:main localhost 8000

Unfortunately, after Ctrl-C and :reload, running it again:

** Exception: bind: resource busy (Address already in use)

This is pretty annoying, because quitting-and-restarting takes a lot of
time since I have many dependencies.

How do you deal with this? Can you propose some working code that can be
wrapped around my main function to make it work?

My first idea is running main in a separate process.

If you have a working idea, please also post it as an answer on
http://stackoverflow.com/questions/15890912/how-do-i-terminate-a-socket-server-in-ghci

Thanks
Manuel Gómez | 11 Apr 08:25 2013
Picon

Re: Web servers: Running them multiple times in a ghci session

On Thu, Apr 11, 2013 at 12:33 AM, Niklas Hambüchen <mail <at> nh2.me> wrote:
>
> I'm writing a web server app, which I run in ghci:
>
> :main localhost 8000
>
> Unfortunately, after Ctrl-C and :reload, running it again:
>
> ** Exception: bind: resource busy (Address already in use)
>
> This is pretty annoying, because quitting-and-restarting takes a lot of
> time since I have many dependencies.
>
> How do you deal with this? Can you propose some working code that can be
> wrapped around my main function to make it work?

Just a guess: it probably has to do with `SO_REUSEADDR`; see this old
thread[1] and the comments on `Network.listenOn`[2].  You most likely
want to set the `ReuseAddr` option to 1 on your socket
with`Network.Socket.setSocketOption`.[3]

[1]: <http://www.haskell.org/pipermail/beginners/2010-June/004334.html>
[2]: <http://hackage.haskell.org/packages/archive/network/2.4.1.2/doc/html/Network.html>
[3]: <http://hackage.haskell.org/packages/archive/network/2.4.1.2/doc/html/Network-Socket.html#t:SocketOption>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
(Continue reading)

Jeremy Shaw | 12 Apr 04:28 2013

Re: Web servers: Running them multiple times in a ghci session

The problem is that ^C only kills the main thread, but does not kill
any child threads that have been spawned.

In happstack the Conf has an optional field where you can supply a
ThreadGroup. When threads are forked they will be registered with the
ThreadGroup, and when you ^C, all those threads can be killed. That
obviously has overhead, so by default we do not use it. But for
development in GHCi it can be a huge time saver.

http://hackage.haskell.org/packages/archive/happstack-server/7.1.7/doc/html/Happstack-Server-Internal-Types.html#t:Conf

Perhaps you can do something similar..

- jeremy

On Thu, Apr 11, 2013 at 12:03 AM, Niklas Hambüchen <mail <at> nh2.me> wrote:
> I'm writing a web server app, which I run in ghci:
>
> :main localhost 8000
>
> Unfortunately, after Ctrl-C and :reload, running it again:
>
> ** Exception: bind: resource busy (Address already in use)
>
> This is pretty annoying, because quitting-and-restarting takes a lot of
> time since I have many dependencies.
>
> How do you deal with this? Can you propose some working code that can be
> wrapped around my main function to make it work?
>
(Continue reading)


Gmane