GHC uses O_NONBLOCK on regular files, which has no effect, and blocks the runtime
This is the outcome of https://mail.haskell.org/pipermail/ghc-devs/2018-May/015749.html
Reading through the code of [readRawBufferPtr](http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.IO.FD.html#readRawBufferPtr) the first line jumped to my eye:
```
| isNonBlocking fd = unsafe_read -- unsafe is ok, it can't block
```
This looks suspicious.
On Linux, if `fd` is a a descriptor to a regular file (on disk, a networked filesystem, or a block device), then `O_NONBLOCK` will have no effect, yet `unsafe_read` is used which will block the running OS thread.
You can read more about `O_NONBLOCK` not working on regular files on Linux here:
- https://www.nginx.com/blog/thread-pools-boost-performance-9x/
- https://stackoverflow.com/questions/8057892/epoll-on-regular-files
- https://jvns.ca/blog/2017/06/03/async-io-on-linux--select--poll--and-epoll/
- https://groups.google.com/forum/\#!topic/comp.os.linux.development.system/K-fC-G6P4EA
And indeed, the following program does NOT keep printing things in the printing thread, and instead blocks for 30 seconds:
```
module Main where
import Control.Concurrent
import Control.Monad
import qualified Data.ByteString as BS
import System.Environment
main :: IO ()
main = do
args <- getArgs
case args of
[file] -> do
forkIO $ forever $ do
putStrLn "still running"
threadDelay 100000 -- 0.1 s
bs <- BS.readFile file
putStrLn $ "Read " ++ show (BS.length bs) ++ " bytes"
_ -> error "Pass 1 argument (a file)"
```
when compiled with
```
~/.stack/programs/x86_64-linux/ghc-8.2.2/bin/ghc --make -O -threaded blocking-regular-file-read-test.hs
```
on my Ubuntu 16.04 and on a 2GB file like
```
./blocking-regular-file-read-test /mnt/images/ubuntu-18.04-desktop-amd64.iso
```
And `strace -f -e open,read` on it shows:
```
open("/mnt/images/ubuntu-18.04-desktop-amd64.iso", O_RDONLY|O_NOCTTY|O_NONBLOCK) = 11
read(11, <unfinished ...>
```
So GHC is trying to use `O_NONBLOCK` on regular files, which cannot work and will block when used through unsafe foreign calls like that.
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | -------------- |
| Version | 8.2.2 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Runtime System |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | lehins, nh2 |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"GHC uses O_NONBLOCK on regular files, which has no effect, and blocks the runtime","status":"New","operating_system":"","component":"Runtime System","related":[],"milestone":"8.6.1","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.2.2","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":["lehins","nh2"],"type":"Bug","description":"This is the outcome of https://mail.haskell.org/pipermail/ghc-devs/2018-May/015749.html\r\n\r\nReading through the code of [http://hackage.haskell.org/package/base-4.11.1.0/docs/src/GHC.IO.FD.html#readRawBufferPtr readRawBufferPtr] the first line jumped to my eye:\r\n\r\n{{{\r\n | isNonBlocking fd = unsafe_read -- unsafe is ok, it can't block\r\n}}}\r\n\r\nThis looks suspicious.\r\n\r\nOn Linux, if `fd` is a a descriptor to a regular file (on disk, a networked filesystem, or a block device), then `O_NONBLOCK` will have no effect, yet `unsafe_read` is used which will block the running OS thread.\r\n\r\nYou can read more about `O_NONBLOCK` not working on regular files on Linux here:\r\n\r\n* https://www.nginx.com/blog/thread-pools-boost-performance-9x/\r\n* https://stackoverflow.com/questions/8057892/epoll-on-regular-files\r\n* https://jvns.ca/blog/2017/06/03/async-io-on-linux--select--poll--and-epoll/\r\n* https://groups.google.com/forum/#!topic/comp.os.linux.development.system/K-fC-G6P4EA\r\n\r\nAnd indeed, the following program does NOT keep printing things in the printing thread, and instead blocks for 30 seconds:\r\n\r\n{{{\r\nmodule Main where\r\n\r\nimport Control.Concurrent\r\nimport Control.Monad\r\nimport qualified Data.ByteString as BS\r\nimport System.Environment\r\n\r\nmain :: IO ()\r\nmain = do\r\n args <- getArgs\r\n case args of\r\n [file] -> do\r\n\r\n forkIO $ forever $ do\r\n putStrLn \"still running\"\r\n threadDelay 100000 -- 0.1 s\r\n bs <- BS.readFile file\r\n putStrLn $ \"Read \" ++ show (BS.length bs) ++ \" bytes\"\r\n\r\n _ -> error \"Pass 1 argument (a file)\"\r\n}}}\r\n\r\nwhen compiled with\r\n\r\n{{{\r\n~/.stack/programs/x86_64-linux/ghc-8.2.2/bin/ghc --make -O -threaded blocking-regular-file-read-test.hs\r\n}}}\r\n\r\non my Ubuntu 16.04 and on a 2GB file like\r\n\r\n{{{\r\n./blocking-regular-file-read-test /mnt/images/ubuntu-18.04-desktop-amd64.iso\r\n}}}\r\n\r\nAnd `strace -f -e open,read` on it shows:\r\n\r\n{{{\r\nopen(\"/mnt/images/ubuntu-18.04-desktop-amd64.iso\", O_RDONLY|O_NOCTTY|O_NONBLOCK) = 11\r\nread(11, <unfinished ...>\r\n}}}\r\n\r\nSo GHC is trying to use `O_NONBLOCK` on regular files, which cannot work and will block when used through unsafe foreign calls like that.\r\n","type_of_failure":"OtherFailure","blocking":[]} -->
issue