haskell-fswatch / hfsnotify

Unified Haskell interface for basic file system notifications
BSD 3-Clause "New" or "Revised" License
136 stars 40 forks source link

no notifications in a forked thread #68

Closed simonmichael closed 6 years ago

simonmichael commented 7 years ago

Thanks for fsnotify. The example works for me, but when I move it into a thread (forkIO) or bound thread (forkOS), I see no events. The executable is built with -threaded and GHC 8 on OSX.

simonmichael commented 7 years ago

More specifically, this prints no events:

{-# LANGUAGE OverloadedStrings #-}

import System.FSNotify
import Control.Concurrent
import Control.Monad

main = do
  withManager $ \mgr -> do
    forkIO $ do
      watchDir
        mgr          -- manager
        "."          -- directory to watch
        (const True) -- predicate
        print        -- action
      forever $ threadDelay 1000000

  forever $ threadDelay 1000000

but this does (moving the withManager into the forked thread):

{-# LANGUAGE OverloadedStrings #-}

import System.FSNotify
import Control.Concurrent
import Control.Monad

main = do
  forkIO $ do
    withManager $ \mgr -> do
      watchDir
        mgr          -- manager
        "."          -- directory to watch
        (const True) -- predicate
        print        -- action
      forever $ threadDelay 1000000

  forever $ threadDelay 1000000

Perhaps docs could speak a bit more about the manager and how it works.

thomasjm commented 6 years ago

In general, context-manager style functions perform their cleanup after the wrapped action is done (see System.IO.withFile for example) and since forkIO returns immediately it makes sense that the manager is cleaned up immediately.

Since the documentation currently says that withManager will "Tear down the WatchManager after the action is complete," I don't think there's anything to add to the docs. I'm going to hit close on this now but if you feel strongly about adding more content to the docs/have some suggestions about what would be helpful I'd be happy to include it. Thanks!