I have the following code for loading contents of the file and parse it with aeson into an object:
loadFromFile filename = do
fileHandle <- openFile ( dataDir ++ "/" ++ filename ) ReadMode
contents <- B.hGetContents fileHandle
let result = fromMaybe initial $! JSON.decode contents in do
hClose fileHandle
return $! result
The problem with this code is that it causes following errors:
filename.json: hGetBufSome: illegal operation (handle is closed)
filename.json: hGetBufSome: illegal operation (handle is closed)
I do not quite understand why it thinks file handle is closed. I am reading it before closing the handle after all. Or is lazy evaluation affecting the order of execution? I tried adding corresponding $! operators to make it evaluate sooner, but it does not help. If I remove hClose fileHandle line, then I start getting sporadic errors of the following kind:
filename.json: openFile: resource busy (file is locked)
If somebody knows how to fix this catch22, I would really appreciate it. Thanks!