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,263
    • Issues 4,263
    • 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
  • #6130

Closed
Open
Opened May 29, 2012 by jmillikin@trac-jmillikin

Weak pointer to MVar is finalized, even though MVar is still accessible

I'm seeing a problem mixing MVar and Weak: even though the MVar is still accessible from a function's scope (and the main thread is blocking on it), the Weak thinks it should be finalized.

Only seems to happen when compiled with -O2.

Platform: 64-bit Linux Reproduced with GHC versions: 6.10.4, 6.12.3, 7.0.4, 7.2.2, 7.4.1

module Main (main) where

import           Control.Concurrent
import           Control.Monad (forever, forM_)
import           Data.IORef
import           System.Mem
import           System.Mem.Weak

dispatchPendingCalls :: IORef [Weak (MVar ())] -> IO ()
dispatchPendingCalls ref = forever $ do
	threadDelay 100000
	
	pending <- atomicModifyIORef ref (\p -> ([], p))
	forM_ pending (\weak -> do
		maybeMVar <- deRefWeak weak
		case maybeMVar of
			Just mvar -> putMVar mvar ()
			Nothing -> putStrLn "dispatchReply: weak mvar is Nothing")

call :: IORef [Weak (MVar ())] -> IO ()
call ref = do
	mvar <- newEmptyMVar
	weak <- mkWeakPtr mvar (Just (putStrLn "call: finalising weak"))
	
	putStrLn "call: about to insert weak into list"
	atomicModifyIORef ref (\p -> (weak : p, ()))
	putStrLn "call: inserted weak into list"
	performGC
	takeMVar mvar
	putStrLn "call: took from mvar"

main :: IO ()
main = do
	pendingCalls <- newIORef []
	_ <- forkIO (dispatchPendingCalls pendingCalls)
	call pendingCalls

Expected output:

$ ghc --make WeakVar.hs
$ ./WeakMvar
call: about to insert weak into list
call: inserted weak into list
call: took from mvar

Actual output:

$ ghc --make -O2 WeakVar.hs
$ ./WeakMvar
call: about to insert weak into list
call: inserted weak into list
call: finalizing weak
dispatchReply: weak mvar is Nothing
Trac metadata
Trac field Value
Version 7.4.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
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#6130