Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Support
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
    • Insights
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
    • Locked Files
  • Issues 3,631
    • Issues 3,631
    • List
    • Boards
    • Labels
    • Milestones
  • Merge Requests 202
    • Merge Requests 202
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Security & Compliance
    • Security & Compliance
    • Dependency List
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #10975

Closed
Open
Opened Oct 16, 2015 by takano-akio@takano-akio
  • Report abuse
  • New issue
Report abuse New issue

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

Related issues

  • Discussion
  • Designs
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
4
Labels
bug incorrect runtime result P::normal Trac import
Assign labels
  • View project labels
Reference: ghc/ghc#10975