fun with sockets (ghc-5.04.1)
Peter Thiemann
thiemann@informatik.uni-freiburg.de
09 Mar 2003 08:04:44 -0800
Hi,
I'm running into a problem using server code (running on a UNIX domain
socket) that I basically snatched from Simon Marlow's web server:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mainLoop sock defUser getMs =
do putStrLn "Accepting connections..."
installHandler sigPIPE Ignore Nothing
loop (0::Int)
where
loop n =
do (connection, peerAddress) <- accept sock
h <- socketToHandle connection ReadWriteMode
forkIO ( (talk n getMs defUser h `finally` goodBye n h)
`Exception.catch`
(\e -> putStrLn ("Exception: " ++ show e))
)
loop (n+1)
goodBye n h =
do putStrLn (show n ++ ": finished")
hClose h
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
After processing a random number of connections, the server dies with
Fail: invalid argument
Action: accept
Reason: Invalid argument
after finishing the last connection. For the record, here is the code
that is executed for each connection:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
talk n getMs user h =
do hSetBuffering h LineBuffering
putStrLn (show n ++ ": ")
str <- hGetLine h
lstr <- hGetLine h
putStrLn ("...: " ++ lstr) -- last output before core dump
let msglength = read (takeWhile isDigit (drop 1 lstr))
rawmsg <- hGetContents h
putStrLn ("Got " ++ show (length msg) ++ " characters")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The machine is
Linux kailua 2.4.18-4GB #1 Wed Mar 27 13:57:05 UTC 2002 i686 unknown
The problem seems to go away if I switch to INET sockets, so I'm also including
my initialization code:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
withSocketsDo $ do
sock <- socket AF_UNIX Stream 0
bindSocket sock (SockAddrUnix path)
listen sock maxListenQueue
getMs <- initializeAll
mainLoop sock defUser getMs
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Peter