[Haskell-cafe] Finalizer registered with System.Mem.Weak.addFinalizer is called surprisingly early

balodja balodja at zlug.asia
Thu Mar 10 07:48:04 CET 2011


Thank you for your answer.

> Does it work if you change:
> 
> >  forM_ [1..1000000] $ \x -> do
> >    send socket (show x)
> >    threadDelay (10*1000)
> 
> to:
> 
> >  forM_ [1..1000000] $ \x -> do
> >    send socket (show x)
> >    touch socket
> >    threadDelay (10*1000)

Yes, that works.

> I'm guessing what happens is that optimizations make the 'Socket'
> constructor go away, so the finalizer runs as soon as it can. The
> 'touch' function might force it to stick around. You'll probably want
> to look at the low-level compiler output to make sure:
> 
> http://hackage.haskell.org/package/ghc-core
> 

As far as I see you are definitely right. Due to good inlining Socket
constructor is not needed in main forM-cycle. After some meditation on
core output I added NOINLINE pragma to function "send" and the problem
vanished. Is NOINLINE pragma a good workaround in such situation?

Thanks,
balodja




More information about the Haskell-Cafe mailing list