avoid STM deadlock

When all worker threads are running and enteringStage is called,
it waits for an idle slot. If all off the other threads then call it in
turn, a deadlock occurrs.

This is the same problem I didn't actually fix in
5a9842d7ed.

Fixed by doing two separate STM transactions, the first replaces its
active thread with an idle thread, and the second waits for another idle
thread. That guarantees there will eventually be an idle thread to find.

The changes to WorkerPool were necessary because it can't add an idle
thread containing the Annex state and go on to run an action using that
same state, so I had to remove the Annex state from IdleWorker.
This commit is contained in:
Joey Hess 2019-06-19 18:07:30 -04:00
parent a0d3a699e2
commit 37d505dd6b
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 91 additions and 59 deletions

View file

@ -129,8 +129,8 @@ finishCommandActions = Annex.getState Annex.workers >>= \case
Nothing -> noop
Just tv -> do
Annex.changeState $ \s -> s { Annex.workers = Nothing }
WorkerPool _ l <- liftIO $ atomically $ takeTMVar tv
forM_ (mapMaybe workerAsync l) $ \aid ->
pool <- liftIO $ atomically $ takeTMVar tv
forM_ (mapMaybe workerAsync $ workerList pool) $ \aid ->
liftIO (waitCatch aid) >>= \case
Left _ -> noop
Right st -> mergeState st