Calling freeHaskellFunctionPtr from C finalizer leads to crashes
|Reported by:||ravi_n||Owned by:|
|Type of failure:||Runtime crash||Test Case:|
|Related Tickets:||Differential Revisions:|
I recently discovered that calling freeHaskellFunctionPtr from a C finalizer (as seems natural to do if the object you're cleaning up on the C side is holding on to a Haskell function wrapper) leads to strange crashes.
The attached program (which is a stress-test loop of allocating function wrappers, using them and then freeing them) illustrates the problem.
You can compile it with:
ghc --make !WrapperTest2.hs HsFunction.c -o !WrapperTest2 -main-is !WrapperTest2
Depending on the platform and compilation flags I've noticed different results. Compiled as above (on Windows) I get: !WrapperTest2.exe: internal error: stg_ap_p_ret
Compiled with -debug I get: !WrapperTest2.exe: internal error: ASSERTION FAILED: file rts\Stable.c, line 224
On Windows, these results do not change if I compile with -threaded.
On Linux, without -threaded, I get the same results as Windows.
However, with -threaded I get different behavior.
With -threaded and without -debug the program eventually deadlocks.
With -threaded and -debug I get the following message: !WrapperTest2: internal error: multiple ACQUIRE_LOCK: rts/Stable.c 284
I'm currently working around the issue by using Haskell finalizers (which don't seem to have this problem), but I thought I should report the bug. I'd also appreciate feedback on whether using Haskell finalizers is actually a safe workaround for this issue or if I've just been lucky so far.