Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,262
    • Issues 4,262
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 419
    • Merge Requests 419
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #7160

Closed
Open
Opened Aug 18, 2012 by Bertram Felgenhauer@int-eReporter

C finalizers are reversed during GC

(See also thread starting at http://www.haskell.org/pipermail/libraries/2012-August/018302.html )

The list of finalizers is reversed during GC (cf. rts/sm/MarkWeak.c), which may cause them to run in the wrong order. The following program reproduces this behaviour.

{-# LANGUAGE ForeignFunctionInterface, MagicHash #-}
import GHC.ForeignPtr
import GHC.Ptr
import System.Mem

-- one should really use own C function rather than this varargs one to avoid
-- possible ABI issues
foreign import ccall "&debugBelch" fun :: FunPtr (Ptr () -> Ptr () -> IO ())

new name = do
    p <- newForeignPtr_ (Ptr name)
    addForeignPtrFinalizerEnv fun (Ptr "finalizer 1 (%s)\n"#) p
    addForeignPtrFinalizerEnv fun (Ptr "finalizer 2 (%s)\n"#) p
    return p

main = do
    p <- new "p"#
    q <- new "q"#
    r <- new "r"#
    performGC -- collect p. finalizer order: 2, then 1.
    print q
    performGC -- collect q. finalizer order: 1, then 2.
              -- expected order: 2, then 1.
    print r
    performGC -- collect r. finalizer order: 2, then 1.
Trac metadata
Trac field Value
Version 7.6.1-rc1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Runtime System
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Assignee
Assign to
7.6.1
Milestone
7.6.1
Assign milestone
Time tracking
None
Due date
None
Reference: ghc/ghc#7160