First, the "bug" part:

None of the libraries that use errno are compatible with concurrency.
When a (lightweight) thread-switch occurs between the call to some foreign function and the corresponding call to Foreign.C.Error.getErrno, we have a problem:

a) the RTS trashes the value of errno
b) another Haskell thread might call a function that sets errno
c) on most platforms, errno is thread-local state, with all the consequences that has for the threaded RTS (bound threads required....)

Now, the "ffi" part:

b) and c) could be solved by using bound threads, but that's probably too inefficient for something that's used as frequently as errno.
Bound threads wouldn't solve a).

I therefore propose that we make the RTS save & restore the value of errno in the TSO, thus making errno "Haskell-Thread-Local-State", and solving all of the above problems.
I also propose adding a note to the FFI spec (or the bound threads document, I'm not sure which is the right place) that says that implementations have to make sure that "Errno" is local to each *Haskell* thread.



FFI mailing list

Reply via email to