Skip to content

Commit 3cc5c14

Browse files
committed
Sort: Cleanup temporary files on failure
1 parent 5fe88ee commit 3cc5c14

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/GHC/RTS/Events/Sort.hs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module GHC.RTS.Events.Sort
66
( GHC.RTS.Events.Sort.sortEvents
77
) where
88

9+
import Control.Exception
10+
import Data.IORef
911
import Data.Traversable
1012
import Data.Coerce
1113
import Data.Function (on)
@@ -46,6 +48,25 @@ instance Ord OnTime where
4648
instance Eq OnTime where
4749
(==) = coerce ((==) `on` evTime)
4850

51+
-- | C
52+
cleanupFiles :: ((FilePath -> IO ()) -> IO a)
53+
-- ^ a continuation accepting an action to register a file to
54+
-- be cleaned up.
55+
-> IO a
56+
cleanupFiles cont =
57+
bracket start finish (cont . register)
58+
where
59+
start :: IO (IORef [FilePath])
60+
start = newIORef []
61+
finish :: IORef [FilePath] -> IO ()
62+
finish ref = do
63+
files <- readIORef ref
64+
mapM_ removeFile files
65+
register :: IORef [FilePath] -> FilePath -> IO ()
66+
register ref path = do
67+
atomicModifyIORef ref (\paths -> (path : paths, ()))
68+
return ()
69+
4970
-- | @sortEvents tmpDir outPath eventlog@ sorts @eventlog@ via on-disk merge
5071
-- sort, using @tmpDir@ for intermediate data. The sorted eventlog is written
5172
-- to @eventlog@.
@@ -54,10 +75,11 @@ sortEvents :: FilePath -- ^ temporary directory
5475
-> EventLog -- ^ eventlog to sort
5576
-> IO ()
5677
sortEvents _tmpDir _outPath (EventLog _ (Data [])) = fail "sortEvents: no events"
57-
sortEvents tmpDir outPath (EventLog hdr (Data events0)) = do
78+
sortEvents tmpDir outPath (EventLog hdr (Data events0)) = cleanupFiles $ \cleanupFile -> do
5879
chunks <- toSortedChunks events0
80+
mapM_ cleanupFile chunks
5981
hdl <- openBinaryFile outPath WriteMode
60-
mergeChunks' hdl chunks
82+
mergeChunks' cleanupFile hdl chunks
6183
hClose hdl
6284
return ()
6385
where
@@ -67,8 +89,8 @@ sortEvents tmpDir outPath (EventLog hdr (Data events0)) = do
6789
. mapM (writeTempChunk . sortEventsInMem)
6890
. chunksOf cHUNK_SIZE
6991

70-
mergeChunks' :: Handle -> S.Seq SortedChunk -> IO ()
71-
mergeChunks' destFile chunks
92+
mergeChunks' :: (FilePath -> IO ()) -> Handle -> S.Seq SortedChunk -> IO ()
93+
mergeChunks' cleanupFile destFile chunks
7294
| S.null chunks =
7395
fail "sortEvents: this can't happen"
7496
| S.length chunks <= fAN_IN = do
@@ -78,10 +100,11 @@ sortEvents tmpDir outPath (EventLog hdr (Data events0)) = do
78100
| otherwise = do
79101
chunksss <- flip mapM (nChunks fAN_IN chunks) $ \fps -> do
80102
(fp, hdl) <- createTempChunk
81-
mergeChunks' hdl fps
103+
cleanupFile fp
104+
mergeChunks' cleanupFile hdl fps
82105
mapM_ removeFile fps
83106
return fp
84-
mergeChunks' destFile (S.fromList chunksss)
107+
mergeChunks' cleanupFile destFile (S.fromList chunksss)
85108

86109
readChunk :: SortedChunk -> IO [Event]
87110
readChunk fp = do

0 commit comments

Comments
 (0)