Skip to content

Commit

Permalink
Demonstrate one aspect of issue haskell#187
Browse files Browse the repository at this point in the history
Due to the nature of directory-based locking, if the process exits
before exception handlers are run, the directory is never removed and
the system remains in a "locked" state indefinitely. This can happen due
to SIGKILL or power failure, as well as a blocked thread when main
exits. This repro follows that last approach, forking a thread and then
letting main exit while that locked thread delays.

On my machine, I get the following output for running issue-187.sh:

$ ./issue-187.sh
+ rm -rf tmp
+ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.2.2
+ cabal --version
cabal-install version 2.0.0.1
compiled using version 2.0.1.1 of the Cabal library
+ cabal sandbox init
Writing a default package environment file to
/Users/michael/Documents/hackage-security/cabal.sandbox.config
Using an existing sandbox located at
/Users/michael/Documents/hackage-security/.cabal-sandbox
+ cabal install ./hackage-security
Resolving dependencies...
In order, the following will be installed:
hackage-security-0.5.2.2 (reinstall)
Warning: Note that reinstalls are always dangerous. Continuing anyway...
Notice: installing into a sandbox located at
/Users/michael/Documents/hackage-security/.cabal-sandbox
Configuring hackage-security-0.5.2.2...
Building hackage-security-0.5.2.2...
Installed hackage-security-0.5.2.2
+ cabal exec -- ghc -hide-all-packages -package hackage-security
-package base -package directory -Wall -Werror issue-187.hs
[1 of 1] Compiling Main             ( issue-187.hs, issue-187.o )
Linking issue-187 ...
+ ./issue-187
+ ./issue-187
issue-187:
/Users/michael/Documents/hackage-security/tmp/hackage-security-lock:
createDirectory: already exists (File exists)
  • Loading branch information
snoyberg committed Feb 13, 2018
1 parent 71a24d6 commit ffb83f9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions issue-187.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Control.Concurrent (MVar, forkIO,
newEmptyMVar,
putMVar, takeMVar,
threadDelay)
import Control.Exception (finally)
import qualified Hackage.Security.Client as HS
import qualified Hackage.Security.Client.Repository.Cache as HS
import qualified Hackage.Security.Util.Path as HS
import System.Directory (canonicalizePath, createDirectoryIfMissing)

update :: MVar () -> FilePath -> IO ()
update baton path = HS.lockCache cache (do
putMVar baton ()
threadDelay 5000000
putStrLn "Leaving update") `finally` putMVar baton ()
where
cache = HS.Cache
{ HS.cacheRoot = HS.fromAbsoluteFilePath path
, HS.cacheLayout = HS.cabalCacheLayout
}

main :: IO ()
main = do
createDirectoryIfMissing True "tmp"
path <- canonicalizePath "tmp"
baton <- newEmptyMVar
_ <- forkIO $ update baton path
takeMVar baton
22 changes: 22 additions & 0 deletions issue-187.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -eux

rm -rf tmp

ghc --version
cabal --version

cabal sandbox init || true
cabal install ./hackage-security

cabal exec -- ghc \
-hide-all-packages \
-package hackage-security \
-package base \
-package directory \
-Wall -Werror \
issue-187.hs

./issue-187
./issue-187

0 comments on commit ffb83f9

Please sign in to comment.