From 8d5ba728e69cc028f4daf77e5af19947d0c515ef Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 19 May 2013 14:16:36 -0400 Subject: Switch to MonadCatchIO-transformers for better handling of state while catching exceptions. As seen in this bug report, the lifted exception handling using the StateT monad throws away state changes when an action throws an exception. http://git-annex.branchable.com/bugs/git_annex_fork_bombs_on_gpg_file/ .. Which can result in cached values being redundantly calculated, or other possibly worse bugs when the annex state gets out of sync with reality. This switches from a StateT AnnexState to a ReaderT (MVar AnnexState). All changes to the state go via the MVar. So when an Annex action is running inside an exception handler, and it makes some changes, they immediately go into affect in the MVar. If it then throws an exception (or even crashes its thread!), the state changes are still in effect. The MonadCatchIO-transformers change is actually only incidental. I could have kept on using lifted-base for the exception handling. However, I'd have needed to write a new instance of MonadBaseControl for the new monad.. and I didn't write the old instance.. I begged Bas and he kindly sent it to me. Happily, MonadCatchIO-transformers is able to derive a MonadCatchIO instance for my monad. This is a deep level change. It passes the test suite! What could it break? Well.. The most likely breakage would be to code that runs an Annex action in an exception handler, and *wants* state changes to be thrown away. Perhaps the state changes leaves the state inconsistent, or wrong. Since there are relatively few places in git-annex that catch exceptions in the Annex monad, and the AnnexState is generally just used to cache calculated data, this is unlikely to be a problem. Oh yeah, this change also makes Assistant.Types.ThreadedMonad a bit redundant. It's now entirely possible to run concurrent Annex actions in different threads, all sharing access to the same state! The ThreadedMonad just adds some extra work on top of that, with its own MVar, and avoids such actions possibly stepping on one-another's toes. I have not gotten rid of it, but might try that later. Being able to run concurrent Annex actions would simplify parts of the Assistant code. --- Command/Add.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Command') diff --git a/Command/Add.hs b/Command/Add.hs index be7c6e75e..543d37136 100644 --- a/Command/Add.hs +++ b/Command/Add.hs @@ -130,8 +130,8 @@ ingest (Just source) = do go k cache = ifM isDirect ( godirect k cache , goindirect k cache ) goindirect (Just (key, _)) _ = do - handle (undo (keyFilename source) key) $ - moveAnnex key $ contentLocation source + catchAnnex (moveAnnex key $ contentLocation source) + (undo (keyFilename source) key) liftIO $ nukeFile $ keyFilename source return $ Just key goindirect Nothing _ = failure "failed to generate a key" @@ -172,7 +172,7 @@ undo :: FilePath -> Key -> IOException -> Annex a undo file key e = do whenM (inAnnex key) $ do liftIO $ nukeFile file - handle tryharder $ fromAnnex key file + catchAnnex (fromAnnex key file) tryharder logStatus key InfoMissing throw e where @@ -184,7 +184,7 @@ undo file key e = do {- Creates the symlink to the annexed content, returns the link target. -} link :: FilePath -> Key -> Bool -> Annex String -link file key hascontent = handle (undo file key) $ do +link file key hascontent = flip catchAnnex (undo file key) $ do l <- inRepo $ gitAnnexLink file key replaceFile file $ makeAnnexLink l -- cgit v1.2.3