At program exit, finalizer runs while foreign function is running
The following code prints "finalized", which means the finalizer runs while the "call" function is still running.
Steps to reproduce:
% ghc finalizer.hs foreign.c -threaded
% ./finalizer
finalizer.hs:
import Control.Concurrent
import Control.Monad
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr
main :: IO ()
main = do
_ <- forkIO $ do
fptr <- newForeignPtr finalizer nullPtr
forever $ withForeignPtr fptr call
threadDelay 1000000
foreign import ccall "&finalizer" finalizer :: FunPtr (Ptr CInt -> IO ())
foreign import ccall "call" call :: Ptr CInt -> IO ()
foreign.c
#include <stdio.h>
int finalized = 0;
void finalizer(int *a)
{
finalized = 1;
puts("finalizer");
}
void call(int *a)
{
int i;
unsigned w = 0;
puts("begin call");
for(i = 0; i < 100000000; i++)
{
if(finalized)
puts("finalized");
w += i;
}
printf("end call: %u\n", w);
}
Trac metadata
Trac field | Value |
---|---|
Version | 7.10.1 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |