-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
openFile is not exception safe #8
Comments
I don't believe the programming model was to rely on finalizers to close handles. The non determinism of when this happens can cause a number of issues. As far as I'm aware of the automatic Handle cleanup is a last ditch effort to prevent resource leaking but not something to be relied upon. At least that's what I've alway used as the model. |
@Mistuke So do you think we just shouldn't put finalizers on handles? |
@treeowl no, we should have them for a resource management point of view. It's always good to try not to leak resources, especially if the program is about to abort. What I'm saying is I don't think they should be relied upon for correctness of your program. In my opinion the finalizers are there to try handle situations where the program is "incorrect". In the sense that it had no deterministic resource management structure. But a programmer shouldn't rely on them to run in order for his program to work. But that's my opinion on the subject and how I've mostly treated them. |
@Mistuke It sounded like you thought it isn't a bug that |
Sorry for the confusion. That That's because I don't think
Isn't a valid expectation to have. But as you say users expect it, so we should try to get it to work as much as possible. |
If you look at what I did in |
I'm still not sure what is your proposal exactly. I don't know what you did in base. |
Here's the code to look at. See the explanation above it. https://hackage.haskell.org/package/base-4.17.0.0/docs/src/GHC.IO.Handle.FD.html#withOpenFile%27 |
Also read the stuff at |
Breadcrumbs:
|
This works: {-# LANGUAGE QuasiQuotes #-}
module Main where
import Control.Concurrent
import Control.Monad
import qualified Data.ByteString.Lazy as BL
import System.File.OsPath
import System.IO
import System.OsPath
main :: IO ()
main = replicateM_ 100000 $ do
let fn = "test.txt"
thr <- forkIO (System.IO.readFile fn >>= putStr)
threadDelay 1
void $ killThread thr This fails with {-# LANGUAGE QuasiQuotes #-}
module Main where
import Control.Concurrent
import Control.Monad
import qualified Data.ByteString.Lazy as BL
import System.File.OsPath
import System.IO
import System.OsPath
main :: IO ()
main = replicateM_ 100000 $ do
let fn = [osp|test.txt|]
thr <- forkIO (System.File.OsPath.readFile fn >>= BL.putStr)
threadDelay 1
void $ killThread thr |
Whoops... It looks like I forgot the part where |
Yes I'm aware we still leak the file descriptors (although Handles don't with the patch). I'll fix that shortly. |
I believe it is fixed now: #11 |
If someone opens a file without masking exceptions, then (on a bad day) the file could be opened but no finalizer created to close it. That breaks the typical user expectation that dropping a
Handle
will eventually close the associated file descriptor. I fixed a related issue inbase
a while back, though I don't remember the details. Arguably the whole idea of automatically closing files is a bad one, but consistency seems important.The text was updated successfully, but these errors were encountered: