Ticket #1364 (closed merge: fixed)
Finalizers not guaranteed to run before the program exits
| Reported by: | tomac@… | Owned by: | igloo |
|---|---|---|---|
| Priority: | normal | Milestone: | 6.10.2 |
| Component: | Runtime System | Version: | 6.6.1 |
| Keywords: | Cc: | pho@…, conal@…, johan.tibell@… | |
| Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
| Type of failure: | Difficulty: | Moderate (less than a day) | |
| Test Case: | Blocked By: | ||
| Blocking: | Related Tickets: |
Description
In the following code (compiled with compiled with ghc --make -fffi -fvia-c Test.hs) the finalizer never gets called, even after replacing printf with something else:
Test.hs:
module Main where
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Marshal.Utils
foreign import ccall safe "ctest.h &ctest" ctestPtr :: FunPtr (Ptr Int -> IO ())
test :: Int -> IO ()
test i = with i test'
where
test' ptr = do fptr <- newForeignPtr ctestPtr ptr
putStrLn "test"
main = do putStrLn "before test..."
test 33
putStrLn "after test..."
ctest.h:
#include <stdio.h>
static inline void ctest( int *i )
{
printf( "finalizer called with: %d\n", *i );
}
I've asked about this on IRC and the Haskell Cafe mailing list and received confirmation that in GHC there is no guarantee that finalizers will run before the program exits:
http://www.haskell.org/pipermail/haskell-cafe/2007-May/025458.html
The FFI addendum to the Haskell 98 report states in section 5.5 that "The only guarantee [for finalizers] is that the finalizer runs before the program terminates".
The same statement is made in GHC documentation for newForeignPtr.
This could be a bug or merely a documentation issue but something should probably be done about it. I suspect it's a bug considering the guarantee specified in the FFI addendum isn't met.

