Foreign calls and periodic alarm signals

TL;DR: Does 'foreign import safe' silence the periodic alarm signals? I received a report on this rather strange bug in 'directory': https://github.com/haskell/directory/issues/35#issuecomment-136890912 I've concluded based on the dtruss log that it's caused by the timer signal that the GHC runtime emits. Somewhere inside the guts of 'realpath' on Mac OS X, there is a function that does the moral equivalent of: while (statfs64(…) && errno == EINTR); On a slow filesystem like SSHFS, this can cause a permanent hang from the barrage of signals. The reporter found that using 'foreign import safe' mitigates the issue. What I'm curious mainly is that: is something that the GHC runtime guarantees -- is using 'foreign import safe' assured to turn off the periodic signals for that thread? I tried reading this article [1], which seems to be the only documentation I could find about this, and it didn't really go into much depth about them. (I also couldn't find any info about how frequently they occur, on which threads they occur, or which specific signal it uses.) I'm also concerned whether there are other foreign functions out in the wild that could suffer the same bug, but remain hidden because they normally complete before the next alarm signal. [1]: https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Signals

quoth Phil Ruffwind
The reporter found that using 'foreign import safe' mitigates the issue. What I'm curious mainly is that: is something that the GHC runtime guarantees -- is using 'foreign import safe' assured to turn off the periodic signals for that thread?
Can't answer that, but I suppose I would recommend it anyway, because unsafe foreign calls cause other OS threads to block in the runtime. So any real I/O might as well be "safe". To reliably avoid these signals, you can turn them off with the -V0 runtime flag. For a UNIX shell example, GHCRTS=-V0 your-command-here. You have to build it with the -rtsopts flag. I suppose there are some consequences to doing that, but I've never noticed anything and haven't heard of anyone else noticing anything.
I'm also concerned whether there are other foreign functions out in the wild that could suffer the same bug, but remain hidden because they normally complete before the next alarm signal.
Sure are, though I don't know of any that have been identified so directly as yours. I mean it sounds like you know where and how it's breaking. Usually we just know something's dying on an interrupt and then think to try turning off the signal barrage. It's interesting that you're getting a stall instead, due to an EINTR loop. Donn

On Wed, Sep 2, 2015 at 7:56 PM, Donn Cave
Sure are, though I don't know of any that have been identified so directly as yours. I mean it sounds like you know where and how it's breaking. Usually we just know something's dying on an interrupt and then think to try turning off the signal barrage. It's interesting that you're getting a stall instead, due to an EINTR loop.
network is moderately infamous for (formerly?) using unsafe calls that block.... -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 2 Sep 2015, at 16:56, Donn Cave
wrote: To reliably avoid these signals, you can turn them off with the -V0 runtime flag. For a UNIX shell example, GHCRTS=-V0 your-command-here. You have to build it with the -rtsopts flag.
That does seem to have fixed it for me, thanks. Hamish

On 02/09/2015 15:42, Phil Ruffwind wrote:
TL;DR: Does 'foreign import safe' silence the periodic alarm signals?
No it doesn't. Perhaps the fact that a safe FFI call may create another worker thread means that the timer signal has gone to the other thread and didn't interrupt the thread making the statfs64() call. There's pthread_setmask() that could help, but it's pretty difficult to do this in a consistent way because we'd have to pthread_setmask() every thread that runs Haskell code, including calls from outside. I'm not sure yet what the right solution is, but a good start would be to open a ticket. Cheers Simon
I received a report on this rather strange bug in 'directory':
https://github.com/haskell/directory/issues/35#issuecomment-136890912
I've concluded based on the dtruss log that it's caused by the timer signal that the GHC runtime emits. Somewhere inside the guts of 'realpath' on Mac OS X, there is a function that does the moral equivalent of:
while (statfs64(…) && errno == EINTR);
On a slow filesystem like SSHFS, this can cause a permanent hang from the barrage of signals.
The reporter found that using 'foreign import safe' mitigates the issue. What I'm curious mainly is that: is something that the GHC runtime guarantees -- is using 'foreign import safe' assured to turn off the periodic signals for that thread?
I tried reading this article [1], which seems to be the only documentation I could find about this, and it didn't really go into much depth about them. (I also couldn't find any info about how frequently they occur, on which threads they occur, or which specific signal it uses.)
I'm also concerned whether there are other foreign functions out in the wild that could suffer the same bug, but remain hidden because they normally complete before the next alarm signal.
[1]: https://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Signals _______________________________________________ ghc-devs mailing list ghc-devs@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs

a good start would be to open a ticket.
Okay, done: https://ghc.haskell.org/trac/ghc/ticket/10840
participants (5)
-
Brandon Allbery
-
Donn Cave
-
Hamish Mackenzie
-
Phil Ruffwind
-
Simon Marlow