1

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>"]
Leo Zhang
  • 3,040
  • 3
  • 24
  • 38
  • It might be binding to different addresses, judging by the [source](https://hackage.haskell.org/package/streaming-commons-0.2.0.0/docs/src/Data-Streaming-Network.html#bindPortGenEx). – Thomas M. DuBuisson May 11 '18 at 21:48
  • Further evidence, when I run your test and netstat: `tcp46 ... *.3000 *.* LISTEN` and `tcp4 ... *.3000 *.* LISTEN` – Thomas M. DuBuisson May 11 '18 at 21:50

1 Answers1

2

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)
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • Does that deserve having an issue opened in warp? That's not what I would consider expected behavior, even if it's documented. Seems like it may even be a potential security issue on a multi-homed machine. – Carl May 11 '18 at 22:00
  • I feel like this is bad behavior, though it was clearly intentional so I'm not sure how much traction you'll get filing an issue. – Thomas M. DuBuisson May 11 '18 at 22:12
  • If "works as intended", we should be able to file bugs against intentions. – luqui May 11 '18 at 22:32
  • Thanks. It is possible to alter the behavior such that it would crash if 3000 on tcp4 is in use? Often time, I don't want the server to be running on on tcp46 – Leo Zhang May 12 '18 at 00:29