when starting the assistant, wait for it to create the shim file, as well as the pid file

fixes a possible race
This commit is contained in:
Joey Hess 2012-07-26 15:28:08 -04:00
parent 6de38a2ca8
commit e79198aacb

View file

@ -31,39 +31,34 @@ seek = [withFlag restartOption $ \restart -> withNothing $ start restart]
start :: Bool -> CommandStart start :: Bool -> CommandStart
start restart = notBareRepo $ do start restart = notBareRepo $ do
f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim f <- liftIO . absPath =<< fromRepo gitAnnexHtmlShim
ok <- liftIO $ doesFileExist f if restart
if restart || not ok
then do then do
stopDaemon stopDaemon
void $ liftIO . catchMaybeIO . removeFile nuke =<< fromRepo gitAnnexPidFile
=<< fromRepo gitAnnexPidFile startassistant f
startassistant else unlessM (checkpid f) $
else do startassistant f
r <- checkpid
when (r == Nothing) $
startassistant
let url = "file://" ++ f let url = "file://" ++ f
ifM (liftIO $ runBrowser url) ifM (liftIO $ runBrowser url)
( stop ( stop
, error $ "failed to start web browser on url " ++ url , error $ "failed to start web browser on url " ++ url
) )
where where
checkpid = do nuke f = void $ liftIO $ catchMaybeIO $ removeFile f
checkpid f = do
pidfile <- fromRepo gitAnnexPidFile pidfile <- fromRepo gitAnnexPidFile
liftIO $ checkDaemon pidfile liftIO $
startassistant = do doesFileExist f <&&> (isJust <$> checkDaemon pidfile)
startassistant f = do
nuke f
{- Fork a separate process to run the assistant, {- Fork a separate process to run the assistant,
- with a copy of the Annex state. -} - with a copy of the Annex state. -}
state <- Annex.getState id state <- Annex.getState id
liftIO $ void $ forkProcess $ liftIO $ void $ forkProcess $
Annex.eval state $ startDaemon True False Annex.eval state $ startDaemon True False
waitdaemon (100 :: Int) waitdaemon f (100 :: Int)
waitdaemon 0 = error "failed to start git-annex assistant" waitdaemon _ 0 = error "failed to start git-annex assistant"
waitdaemon n = do waitdaemon f n = unlessM (checkpid f) $ do
r <- checkpid -- wait 0.1 seconds before retry
case r of liftIO $ threadDelay 100000
Just _ -> return () waitdaemon f (n - 1)
Nothing -> do
-- wait 0.1 seconds before retry
liftIO $ threadDelay 100000
waitdaemon (n - 1)