Fix crash in server (accessing deleted room)
authorunc0rr
Thu, 07 Apr 2011 21:43:43 +0400
changeset 5119 f475e10c4081
parent 5118 9cfdb9bc6140
child 5120 c4d5ddf939fc
Fix crash in server (accessing deleted room)
gameServer/Actions.hs
gameServer/Store.hs
gameServer/hedgewars-server.hs
gameServer/stresstest.hs
--- a/gameServer/Actions.hs	Thu Apr 07 11:30:56 2011 -0400
+++ b/gameServer/Actions.hs	Thu Apr 07 21:43:43 2011 +0400
@@ -211,7 +211,8 @@
         else
         mapM_ processAction [AnswerClients chans ["LEFT", clNick, msg], RemoveClientTeams ci]
 
-    io $ do
+    -- when not removing room
+    when (not master || (gameProgress && playersNum > 1)) . io $ do
         modifyRoom rnc (\r -> r{
                 playersIn = playersIn r - 1,
                 readyPlayers = if ready then readyPlayers r - 1 else readyPlayers r
@@ -408,7 +409,7 @@
         [
             AnswerClients [sendChan cl] ["CONNECTED", "Hedgewars server http://www.hedgewars.org/", serverVersion]
             , CheckBanned
---            , AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl)
+            , AddIP2Bans (host cl) "Reconnected too fast" (addUTCTime 10 $ connectTime cl)
         ]
 
 
--- a/gameServer/Store.hs	Thu Apr 07 11:30:56 2011 -0400
+++ b/gameServer/Store.hs	Thu Apr 07 21:43:43 2011 +0400
@@ -69,23 +69,15 @@
     when (IntSet.null freeElems) $ growStore m
 
 
-truncateStore :: MStore e -> IO ()
-truncateStore (MStore ref) = do
-    (busyElems, freeElems, arr) <- readIORef ref
-    (_, m') <- IOA.getBounds arr
-    let newM' = truncFunc (m' + 1) - 1
-    newArr <- IOA.newArray_ (0, newM')
-    sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems]
-    writeIORef ref (busyElems, freeElems `IntSet.difference` IntSet.fromAscList [newM'..m'+1], newArr)
-
-
 truncateIfNeeded :: MStore e -> IO ()
-truncateIfNeeded m@(MStore ref) = do
+truncateIfNeeded (MStore ref) = do
     (busyElems, _, arr) <- readIORef ref
     (_, m') <- IOA.getBounds arr
     let newM' = truncFunc (m' + 1) - 1
-    let allLessM = all (< newM') $ IntSet.elems busyElems
-    when (newM' < m' && allLessM) $ truncateStore m
+    when (newM' < m' && (not $ IntSet.null busyElems) && IntSet.findMax busyElems <= newM') $ do
+        newArr <- IOA.newArray_ (0, newM')
+        sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- IntSet.toList busyElems]
+        writeIORef ref (busyElems, IntSet.fromAscList [0..newM'] `IntSet.difference` busyElems, newArr)
 
 
 addElem :: MStore e -> e -> IO ElemIndex
--- a/gameServer/hedgewars-server.hs	Thu Apr 07 11:30:56 2011 -0400
+++ b/gameServer/hedgewars-server.hs	Thu Apr 07 21:43:43 2011 +0400
@@ -24,7 +24,7 @@
 setupLoggers :: IO ()
 setupLoggers =
     updateGlobalLogger "Clients"
-        (setLevel INFO)
+        (setLevel NOTICE)
 
 
 server :: ServerInfo -> IO ()
--- a/gameServer/stresstest.hs	Thu Apr 07 11:30:56 2011 -0400
+++ b/gameServer/stresstest.hs	Thu Apr 07 21:43:43 2011 +0400
@@ -14,12 +14,13 @@
 import System.Posix
 #endif
 
-session1 nick room = ["NICK", nick, "", "PROTO", "32", "", "PING", "", "CHAT", "lobby 1", "", "CREATE_ROOM", room, "", "CHAT", "room 1", "", "QUIT", "creator", ""]
-session2 nick room = ["NICK", nick, "", "PROTO", "32", "", "LIST", "", "JOIN_ROOM", room, "", "CHAT", "room 2", "", "PART", "", "CHAT", "lobby after part", "", "QUIT", "part-quit", ""]
-session3 nick room = ["NICK", nick, "", "PROTO", "32", "", "LIST", "", "JOIN_ROON", room, "", "CHAT", "room 2", "", "QUIT", "quit", ""]
+session 0 nick room = ["NICK", nick, "", "PROTO", "38", "", "PING", "", "CHAT", "lobby 1", "", "PONG", "", "CREATE_ROOM", room, "", "CHAT", "room 1", "", "QUIT", "creator", ""]
+session 1 nick room = ["NICK", nick, "", "PROTO", "38", "", "LIST", "", "JOIN_ROOM", room, "", "PONG", "", "CHAT", "room 2", "", "PART", "", "CHAT", "lobby after part", "", "QUIT", "part-quit", ""]
+session 2 nick room = ["NICK", nick, "", "PROTO", "38", "", "LIST", "", "JOIN_ROOM", room, "", "PONG", "", "CHAT", "room 2", "", "QUIT", "quit", ""]
+session 3 nick room = ["NICK", nick, "", "PROTO", "38", "", "CHAT", "lobby 1", "", "CREATE_ROOM", room, "", "", "PONG", "CHAT", "room 1", "", "PART", "creator", "", "QUIT", "part-quit", ""]
 
 emulateSession sock s = do
-    mapM_ (\x -> hPutStrLn sock x >> hFlush sock >> randomRIO (1000000::Int, 3000000) >>= threadDelay) s
+    mapM_ (\x -> hPutStrLn sock x >> hFlush sock >> randomRIO (100000::Int, 600000) >>= threadDelay) s
     hFlush sock
     threadDelay 225000
 
@@ -27,22 +28,22 @@
     putStrLn "Start"
     sock <- connectTo "127.0.0.1" (PortNumber 46631)
 
-    num1 <- randomRIO (70000::Int, 70100)
-    num2 <- randomRIO (0::Int, 2)
-    num3 <- randomRIO (0::Int, 5)
+    num1 <- randomRIO (100000::Int, 101000)
+    num2 <- randomRIO (0::Int, 3)
+    num3 <- randomRIO (0::Int, 1000)
     let nick1 = 'n' : show num1
-    let room1 = 'r' : show num2
-    case num2 of 
-        0 -> emulateSession sock $ session1 nick1 room1
-        1 -> emulateSession sock $ session2 nick1 room1
-        2 -> emulateSession sock $ session3 nick1 room1
+    let room1 = 'r' : show num3
+    emulateSession sock $ session num2 nick1 room1
     hClose sock
     putStrLn "Finish"
 
 forks = forever $ do
-    delay <- randomRIO (0::Int, 90000)
-    threadDelay delay
-    forkIO testing
+    delays <- randomRIO (0::Int, 2)
+    replicateM 200 $
+        do
+        delay <- randomRIO (delays * 20000::Int, delays * 20000 + 50000)
+        threadDelay delay
+        forkIO testing
 
 main = withSocketsDo $ do
 #if !defined(mingw32_HOST_OS)