haskell-fswatch / hfsnotify

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

Windows error tag (-1) is outside of enumeration's range (0,4) #113

Open supermario opened 2 months ago

supermario commented 2 months ago

I've had users report the following crashes happen occasionally in a scenario where we're using fsnotify to watch the disk for file changes:

lamdera.exe: toEnum{Action}: tag (-1) is outside of enumeration's range (0,4)
CallStack (from HasCallStack):
  error, called at win-src\System\Win32\FileNotify.hsc:46:34 in fsnotify-0.4.1.0-7SQlBibA4qu3CuGnOcBOvF:System.Win32.FileNotify

I'm guessing this is related to:

https://github.com/haskell-fswatch/hfsnotify/blob/cff88823102416939a2faa01afd7913a3476cc0c/win-src/System/Win32/FileNotify.hsc#L42-L46

As it's the only place toEnum is used in this package. But I'm unsure what the -1 value would indicate in Windows or how to debug further.

Would be happy to add debugging/instrumentation code if someone could provide some guidance, but otherwise we don't currently have a sure-fire way of replicating the issue reliably.

thomasjm commented 2 months ago

I see another toEnum usage a few lines down:

https://github.com/haskell-fswatch/hfsnotify/blob/cff88823102416939a2faa01afd7913a3476cc0c/win-src/System/Win32/FileNotify.hsc#L57

It looks like Windows gives us action enums with a value between 1 and 5 inclusive, see here.

It appears that this line is converting from the 1-based Windows indexing to the 0-based Haskell Enum indexing. But somehow Windows is sending 0 for the action, resulting in a call to (toEnum (-1))?

thomasjm commented 2 months ago

If you have a Windows dev machine handy, maybe you can load FileNotify.hsc in GHCi and check that the enums (FileAction and Action) have the expected values?