Haskell: openFile for multiple handles (W+R+R+R...) w/o "file is locked"

Viewed 218

I am working on file-backed queue in Haskell (pub/sub + storage).

My idea is to have a log file with W+R+R+R handles to it:

  • pub: one W handle, for writing/appending
  • sub: multiple R for tailing and random seeking to past entries

However, opening two handles (R+W) to the same file doesn't work with GHC:

#!/usr/bin/env stack
-- stack --resolver lts-13.0 --install-ghc runghc

module Main where

import System.IO

main :: IO ()
main = do
  let path = "file.txt"

  _ <- openFile path WriteMode
  _ <- openFile path ReadMode -- throws *** Exception: file.txt: openFile: resource busy (file is locked)
  _ <- openFile path ReadMode
  _ <- openFile path ReadMode

  return ()

File locking section starts promising:

Implementations should enforce as far as possible, at least locally to the Haskell process, multiple-reader single-writer locking on files.

... but then says:

That is, there may either be many handles on the same file which manage input, or just one handle on the file which manages output.

Questions:

  • why is there such limitation?
  • how to have W+R+R+R handles to a file in Haskell/GHC?
0 Answers
Related