fix transfer slots blocking and refilling when transfers are stopped

There's a bug, if a transfer process notices it needs to do nothing,
it never starts the transfer, so the slot is never freed.
This commit is contained in:
Joey Hess 2012-07-06 21:45:08 -06:00
parent d954a0ce59
commit cc6f660752
3 changed files with 27 additions and 14 deletions

View file

@ -123,7 +123,7 @@ startDaemon assistant foreground
, pushThread st dstatus commitchan pushmap , pushThread st dstatus commitchan pushmap
, pushRetryThread st pushmap , pushRetryThread st pushmap
, mergeThread st , mergeThread st
, transferWatcherThread st dstatus , transferWatcherThread st dstatus transferslots
, transfererThread st dstatus transferqueue transferslots , transfererThread st dstatus transferqueue transferslots
, daemonStatusThread st dstatus , daemonStatusThread st dstatus
, sanityCheckerThread st dstatus transferqueue changechan , sanityCheckerThread st dstatus transferqueue changechan

View file

@ -10,6 +10,7 @@ module Assistant.Threads.TransferWatcher where
import Common.Annex import Common.Annex
import Assistant.ThreadedMonad import Assistant.ThreadedMonad
import Assistant.DaemonStatus import Assistant.DaemonStatus
import Assistant.TransferSlots
import Logs.Transfer import Logs.Transfer
import Utility.DirWatcher import Utility.DirWatcher
import Utility.Types.DirWatcher import Utility.Types.DirWatcher
@ -19,12 +20,12 @@ import Data.Map as M
{- This thread watches for changes to the gitAnnexTransferDir, {- This thread watches for changes to the gitAnnexTransferDir,
- and updates the DaemonStatus's map of ongoing transfers. -} - and updates the DaemonStatus's map of ongoing transfers. -}
transferWatcherThread :: ThreadState -> DaemonStatusHandle -> IO () transferWatcherThread :: ThreadState -> DaemonStatusHandle -> TransferSlots -> IO ()
transferWatcherThread st dstatus = do transferWatcherThread st dstatus transferslots = do
g <- runThreadState st $ fromRepo id g <- runThreadState st $ fromRepo id
let dir = gitAnnexTransferDir g let dir = gitAnnexTransferDir g
createDirectoryIfMissing True dir createDirectoryIfMissing True dir
let hook a = Just $ runHandler st dstatus a let hook a = Just $ runHandler st dstatus transferslots a
let hooks = mkWatchHooks let hooks = mkWatchHooks
{ addHook = hook onAdd { addHook = hook onAdd
, delHook = hook onDel , delHook = hook onDel
@ -32,25 +33,25 @@ transferWatcherThread st dstatus = do
} }
void $ watchDir dir (const False) hooks id void $ watchDir dir (const False) hooks id
type Handler = ThreadState -> DaemonStatusHandle -> FilePath -> Maybe FileStatus -> IO () type Handler = ThreadState -> DaemonStatusHandle -> TransferSlots -> FilePath -> Maybe FileStatus -> IO ()
{- Runs an action handler. {- Runs an action handler.
- -
- Exceptions are ignored, otherwise a whole thread could be crashed. - Exceptions are ignored, otherwise a whole thread could be crashed.
-} -}
runHandler :: ThreadState -> DaemonStatusHandle -> Handler -> FilePath -> Maybe FileStatus -> IO () runHandler :: ThreadState -> DaemonStatusHandle -> TransferSlots -> Handler -> FilePath -> Maybe FileStatus -> IO ()
runHandler st dstatus handler file filestatus = void $ do runHandler st dstatus transferslots handler file filestatus = void $ do
either print (const noop) =<< tryIO go either print (const noop) =<< tryIO go
where where
go = handler st dstatus file filestatus go = handler st dstatus transferslots file filestatus
{- Called when there's an error with inotify. -} {- Called when there's an error with inotify. -}
onErr :: Handler onErr :: Handler
onErr _ _ msg _ = error msg onErr _ _ _ msg _ = error msg
{- Called when a new transfer information file is written. -} {- Called when a new transfer information file is written. -}
onAdd :: Handler onAdd :: Handler
onAdd st dstatus file _ = case parseTransferFile file of onAdd st dstatus _ file _ = case parseTransferFile file of
Nothing -> noop Nothing -> noop
Just t -> runThreadState st $ go t =<< checkTransfer t Just t -> runThreadState st $ go t =<< checkTransfer t
where where
@ -64,7 +65,7 @@ onAdd st dstatus file _ = case parseTransferFile file of
- to avoid zombies. - to avoid zombies.
-} -}
onDel :: Handler onDel :: Handler
onDel st dstatus file _ = case parseTransferFile file of onDel st dstatus transferslots file _ = case parseTransferFile file of
Nothing -> noop Nothing -> noop
Just t -> maybe noop waitchild Just t -> maybe noop waitchild
=<< runThreadState st (removeTransfer dstatus t) =<< runThreadState st (removeTransfer dstatus t)
@ -73,6 +74,8 @@ onDel st dstatus file _ = case parseTransferFile file of
| shouldWait info = case transferPid info of | shouldWait info = case transferPid info of
Nothing -> noop Nothing -> noop
Just pid -> do Just pid -> do
void $ getProcessStatus True False pid void $ tryIO $
getProcessStatus True False pid
runThreadState st invalidateCache runThreadState st invalidateCache
transferComplete transferslots
| otherwise = noop | otherwise = noop

View file

@ -24,7 +24,17 @@ newTransferSlots :: IO TransferSlots
newTransferSlots = newQSemN numSlots newTransferSlots = newQSemN numSlots
{- Waits until a transfer slot becomes available, and runs a transfer {- Waits until a transfer slot becomes available, and runs a transfer
- action in the slot. - action in the slot. If the action throws an exception, its slot is
- freed here, otherwise it should be freed by the TransferWatcher when
- the transfer is complete.
-} -}
inTransferSlot :: TransferSlots -> IO a -> IO a inTransferSlot :: TransferSlots -> IO a -> IO a
inTransferSlot s = bracket_ (waitQSemN s 1) (signalQSemN s 1) inTransferSlot s a = bracketOnError start abort run
where
start = waitQSemN s 1
abort = const $ transferComplete s
run = const a
{- Call when a transfer is complete. -}
transferComplete :: TransferSlots -> IO ()
transferComplete s = signalQSemN s 1