Skip to content

Commit 060a04c

Browse files
committed
Action registry: run delayed actions in FIFO order instead of LIFO
Say we have two delayed actions we want to register in the following order: close a file handle, and remove the corresponding file. When the action registry is committed, then previously we would run the actions in LIFO order. This leads to an error because we can't remove a file while we also have an open handle to that file. Now that we run the actions in FIFO order, it works correctly.
1 parent a40e328 commit 060a04c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src-control/Control/ActionRegistry.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ modifyWithActionRegistry_ getSt putSt action =
142142
can register delayed (commit) and rollback actions. The delayed actions are
143143
all executed at the end if the transaction scope is exited successfully, but
144144
if an exception is thrown (sync or async) then the rollback actions are
145-
executed instead, and the exception is propagated. Delay or rollback actions
146-
are executed in the reverse order in which they were registered, which is the
147-
natural nesting order when considered as bracketing.
145+
executed instead, and the exception is propagated.
146+
147+
* Rollback actions are executed in the reverse order in which they were
148+
registered, which is the natural nesting order when considered as bracketing.
149+
150+
* Delayed actions are executed in the same order in which they are registered.
148151
-}
149152

150153
-- | Registry of monadic actions supporting rollback actions and delayed actions
@@ -247,8 +250,8 @@ unsafeFinaliseActionRegistry reg ec = case ec of
247250
unsafeCommitActionRegistry :: (PrimMonad m, MonadCatch m) => ActionRegistry m -> m ()
248251
unsafeCommitActionRegistry reg = do
249252
as <- readMutVar (registryDelay reg)
250-
-- Run actions in LIFO order
251-
r <- runActions as
253+
-- Run actions in FIFO order
254+
r <- runActions (reverse as)
252255
case NE.nonEmpty r of
253256
Nothing -> pure ()
254257
Just exceptions -> throwIO (CommitActionRegistryError exceptions)

0 commit comments

Comments
 (0)