[Haskell-cafe] Discussion: The CLOEXEC problem

Alexander Kjeldaas alexander.kjeldaas at gmail.com
Sun Aug 30 13:19:13 UTC 2015

The directory is irrelevant.  fork() + exec() is not an atomic operation:

* Thread 1 writes its file completely (opens and closes an Fd 1, using
* Thread 2 starts writing its file (Fd 2 open for writing, using O_CLOEXEC)
* Thread 1 starts executing "myBinary" by calling *fork()*. Fd 2 is
inherited by the child process
* Thread 2 finishes writing (closes its Fd 2)
* Thread 2 executes "myBinary", which fails with `Text file busy`
because an Fd is still open to it in the child of Process 1
* Thread 1 executes "myBinary" (calling exec()). Fd 2 is automatically
closed during exec(), but it's too late.

You need the file descriptor to not be inherited by a child process, which
is != from O_CLOEXEC.


On Fri, Aug 28, 2015 at 10:14 PM, Brandon Allbery <allbery.b at gmail.com>

> On Fri, Jul 24, 2015 at 6:29 PM, Alexander Kjeldaas <
> alexander.kjeldaas at gmail.com> wrote:
>> I think CLOEXEC should be the default, but it doesn't seem to solve your
>> problem. What if thread 2 executes "myBinary" before thread 1 called exec()?
> I think you missed that each thread is using its own temporary directory
> --- they're not all running at the same time in the same directory, which
> would be pretty much guaranteed to fail.
> --
> brandon s allbery kf8nh                               sine nomine
> associates
> allbery.b at gmail.com
> ballbery at sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150830/7769279e/attachment.html>

More information about the Haskell-Cafe mailing list