commercialhaskell / commercialhaskell/stack

Stack test terminates test process on Ctrl+C, preventing resource cleanup

Open
#5,144 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
4.1k
Forks
850
Avg merge
10h 37m
Merged PRs (30d)
4

Description

I'm copying this mostly verbatim from the [StackOverflow question](https://stackoverflow.com/questions/59729290/why-does-haskells-bracket-function-work-in-executables-but-fail-to-clean-up-in) I filed.

I'm seeing a very strange behavior where Haskell's `bracket` function is behaving differently depending on whether `stack run` or `stack test` is used.

Consider the following code, where two nested brackets are used to create and clean up Docker containers:

```haskell
module Main where

import Control.Concurrent
import Control.Exception
import System.Process

main :: IO ()
main = do
bracket (callProcess "docker" ["run", "-d", "--name", "container1", "registry:2"])
(\() -> do
putStrLn "Outer release"
callProcess "docker" ["rm", "-f", "container1"]
putStrLn "Done with outer release"
)
(\() -> do
bracket (callProcess "docker" ["run", "-d", "--name", "container2", "registry:2"])
(\() -> do
putStrLn "Inner release"
callProcess "docker" ["rm", "-f", "container2"]
putStrLn "Done with inner release"
)
(\() -> do
putStrLn "Inside both brackets, sleeping!"
threadDelay 300000000
)
)

```

When I run this with `stack run` and interrupt with `Ctrl+C`, I get the expected output:

```
Inside both brackets, sleeping!
^CInner release
container2
Done with inner release
Outer release
container1
Done with outer release
```

And I can verify that both Docker containers are created and then removed.

However, if I paste this exact same code into a test and run `stack test`, only (part of) the first cleanup happens:

```
Inside both brackets, sleeping!
^CInner release
container2
```

This results in a Docker container left running on my machine. What's going on?

* I've made sure that the exact same `ghc-options` are passed to both.
* Full demonstration repo here: https://github.com/thomasjm/bracket-issue
* Interestingly, if I dig up the test binary and run it manually (i.e. `./.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/bracket-test-test/bracket-test-test`), the problem does not occur. Thus, it seems to be something specific to `stack test`, which is why I'm filing it here as a bug.

### Stack version

```
$ stack --version
Version 2.1.3, Git revision 636e3a759d51127df2b62f90772def126cdf6d1f (7735 commits) x86_64 hpack-0.31.2
```

### Method of installation

* Official binary, downloaded from stackage.org or fpcomplete's package repository

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.