Opened 7 years ago

Closed 6 years ago

#7646 closed bug (fixed)

resource busy (file is locked) with multi-threaded file ops

Reported by: StefanWehr Owned by: simonmar
Priority: high Milestone: 7.6.3
Component: libraries/base Version: 7.6.2
Keywords: Cc: leuschner@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The sample program attached creates 10 worker threads, each of which takes a different file name. Each worker thread then writes the file, reads the file, writes the file and so on. File operations use *strict IO*.

When compiled without -threaded everything is ok, that is, the program goes on forever without any error messages.

But with -threaded, the program quickly fails with ERROR in worker 4: 4: openBinaryFile: resource busy (file is locked).

Tested under Mac OSX 10.8.2 and Linux. Tested with both GHC 7.6.1 and 7.6.2.

I could reproduce the bug without +RTS -N -RTS and with this RTS option. A colleague of mine reports that it needs +RTS -N2 -RTS to reproduce the bug.

Attachments (1)

GhcBug.hs (1.1 KB) - added by StefanWehr 7 years ago.
Testcase for reproducing the bug

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by StefanWehr

Attachment: GhcBug.hs added

Testcase for reproducing the bug

comment:1 Changed 7 years ago by dleuschner

Cc: leuschner@… added

comment:2 Changed 7 years ago by carter

I can replicate the bug on my machine.

Interestingly enough, when I use BS.appendFile instead of BS.writeFile, the bug doesn't happen!

I will try to use that information to sort out what may be happening. (or at least attempt to)

comment:3 Changed 7 years ago by dleuschner


Thank you for looking into this issue, Carter! I assume it might affect more users than we might think, because the error message is typically associated with the lazy-IO problem of first (incompletely) reading and then writing.

For me it still doesn't work with appendFile. The output still is:

$ ghc -threaded -rtsopts --make GhcBug.hs && ./GhcBug +RTS -N2
started worker 3
started worker 2
started worker 5
started worker 7
started worker 9
started worker 1
started worker 4
started worker 6
started worker 8
started worker 10
GhcBug: ERROR in worker 1: 1: openBinaryFile: resource busy (file is locked)
ERROR in worker 1: 1: openBinaryFile: resource busy (file is locked)

comment:4 Changed 7 years ago by simonmar

Component: Runtime Systemlibraries/base
difficulty: Unknown
Milestone: 7.6.3
Owner: set to simonmar
Priority: normalhigh

Fix coming, thanks for the report.

comment:5 Changed 7 years ago by simonmar

Status: newmerge
commit fbd4b4c2587636811d464040e349d81fac9e1121
Author: Simon Marlow <>
Date:   Mon Feb 4 12:08:00 2013 +0000

    Release the FD lock before calling close() (#7646)

comment:6 Changed 7 years ago by dleuschner

Thank you very much, Simon! We will miss you! :-)

comment:7 Changed 6 years ago by thoughtpolice

Resolution: fixed
Status: mergeclosed

Fixed in 7.8/HEAD.

Note: See TracTickets for help on using tickets.