Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Register
  • Sign in
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
    • Locked files
  • Issues 5.5k
    • Issues 5.5k
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 635
    • Merge requests 635
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
    • Test cases
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Model experiments
  • Analytics
    • Analytics
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell CompilerGlasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #1364

Finalizers not guaranteed to run before the program exits

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.

Trac metadata
Trac field Value
Version 6.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Unknown
Architecture Unknown
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking