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-serv... Thanks

On Thu, Apr 11, 2013 at 12:33 AM, Niklas Hambüchen
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... [3]: http://hackage.haskell.org/packages/archive/network/2.4.1.2/doc/html/Network...

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/...
Perhaps you can do something similar..
- jeremy
On Thu, Apr 11, 2013 at 12:03 AM, Niklas Hambüchen
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-serv...
Thanks
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Jeremy Shaw
-
Manuel Gómez
-
Niklas Hambüchen