Leo Zhang
Leo Zhang

Reputation: 3230

scotty doesn't detect if the port is already in use?

I'm running with this example. And it works. However, if I ran another instance, I expect it to crash with an exception, but didn't. The expected exception should say something like "Port 3000 already in use", which is a similar error when you run two python -m SimpleHTTPServer 8000 in different terminals.

{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty

import Data.Monoid (mconcat)

main = scotty 3000 $
    get "/:word" $ do
        beam <- param "word"
        html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]

Upvotes: 1

Views: 188

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

This occurs because the Data.Streaming.Network library used by scotty (well, by warp, which is used by scotty) acquires a list of possible addresses which spans both IPv4 and IPv6 then tries to bind to ports on each of these addresses one at a time, discarding any IO exceptions.

With two scotty instances on port 3000 I see:

% netstat -an | grep 3000
tcp46      0      0  *.3000                 *.*                    LISTEN
tcp4       0      0  *.3000                 *.*                    LISTEN

Trying a third scotty instances I see:

% ./x
Setting phasers to stun... (port 3000) (ctrl-c to quit)
x: Network.Socket.bind: resource busy (Address already in use)

Upvotes: 2

Related Questions