Proposal: Stop enforcing single-writer-multi-reader file access

Duncan Coutts duncan.coutts at googlemail.com
Tue Nov 8 14:07:23 CET 2011


On Tue, 2011-11-08 at 12:04 +0000, Duncan Coutts wrote:
> On Tue, 2011-11-08 at 11:54 +0000, Duncan Coutts wrote:
> > On Wed, 2011-10-26 at 23:59 +0100, Ian Lynagh wrote:
> > > Hi all,
> > > 
> > > Haskell currently requires that multiple-reader single-writer locking is
> > > used on files. I understand the motivation for this was to try to
> > > protect people from running into problems due to lazy IO, but in
> > > practice I think it just causes more problems than it solves.
> 
> > > I propose that we remove all the automatic locking from the libraries,
> > > and let the user do any locking that they wish to do themselves.
> 
> > I have to say, I'm a little reticent about this.
> > 
> > Certainly it's useful to be able deliberately to have multiple writers,
> > or to be able deliberately to read a file while also writing to it. But
> > doing so accidentally seems like a bug waiting to happen doesn't it?
> 
> BTW, I realise this proposal has received a good deal of support,
> however I suspect this is from people who are thinking about the current
> inability to opt-out from locking in the various cases where it would be
> useful for them to do so.

I should also point out that in the referenced ticket

http://hackage.haskell.org/trac/ghc/ticket/4363

nobody was asking to remove all locking. The request was to change the
behaviour on windows to match that of unix which is felt to be more
useful.

That is, to change from OS-wide locking to intra-process locking. Both
are compatible with the H98/2010 spec, but the latter is preferable as a
default.

Additionally, there is clearly value in being able to opt out of all
locking.

I have a suggestion for how that should be done. Not so long ago we
cleaned up the messy System.Process API by adding:

createProcess :: CreateProcess -> IO (...stuff...)

The CreateProcess record has a whole load of parameters and we provide
functions for creating simple default values of these parameters, e.g.

proc :: FilePath -> [String] -> CreateProcess

Crucially, if you want to customise anything, e.g. specify cwd or env
vars then you can do so easily using record update syntax. Also, we can
add new parameters to CreateProcess without breaking old programs.

We should take the same approach with openFile. Currently we have:

openFile :: FilePath -> IOMode -> IO Handle

I suggest changing the IOMode for a record of options which would
include the IO mode (read/write/read-write/etc), and other things like
file locking, default create permissions, create exclusive etc. Again,
we'd use a default value and override extra options as necessary.

Duncan




More information about the Libraries mailing list