Skip to content
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

removeFile did not remove file or throw errors. #12

Closed
Magicloud opened this issue Dec 23, 2014 · 10 comments
Closed

removeFile did not remove file or throw errors. #12

Magicloud opened this issue Dec 23, 2014 · 10 comments
Labels
type: x-intended-behavior The described behavior is working as intended.

Comments

@Magicloud
Copy link

In the following code, in withTemp function, after removeFile fp, the temp file was still there. I tried to use try to catch IOException that may throw by removeFile. I just got Right ().

It is Ubuntu Trusty, ghc 7.8.3, directory 1.2.1.0.

import Network.Curl
import System.Posix.Temp
import System.Directory
import System.IO
import Text.HTML.DOM
import Text.XML
import System.FilePath
import Control.Exception

type PostField = (String, String)

withTemp :: String -> (FilePath -> IO a) -> IO a
withTemp prefix f = do
  tmp <- getTemporaryDirectory
  (fp, h) <- mkstemp $ tmp </> prefix
  hClose h
  finally (f fp) $ removeFile fp

curl :: (CurlBuffer ty) => URLString -> Maybe [PostField] -> IO (Either CurlCode ty)
curl url values = do
  let mPf = fmap (map (\(k, v) -> k ++ "=" ++ v)) values
  (cc, ty) <- withTemp "Cookie" $ \tmp -> withCurlDo $
    let curlOpts_ = [ CurlHttpAuth [HttpAuthAny]
                    , CurlUserPwd ":"
                    , CurlCookieFile tmp
                    , CurlCookieJar tmp
                    , CurlFollowLocation True
                    , CurlUnrestrictedAuth True ]
        curlOpts = case mPf of
                     Nothing -> curlOpts_
                     Just pf -> (CurlPostFields pf) : curlOpts_
    in curlGetString_ url curlOpts
  return $ if cc == CurlOK
              then Right ty
              else Left cc
@hvr hvr added the type: a-bug The described behavior is not working as intended. label Jan 16, 2015
@hvr
Copy link
Member

hvr commented Jan 16, 2015

I wonder if this is related to #4

@Rufflewind
Copy link
Member

I tried the test program and I don't believe the problem is related to directory. The temporary file appears to have been created asynchronously, long after removeFile has completed. There is a roughly 3 second delay before the file appears on the filesystem even though the function has completed execution.

@UnkindPartition
Copy link

Why would the file be created asynchronously?

Anyway, I can't reproduce the issue on Fedora 21 (and same ghc/directory versions as OP's) with

main = curl "http://google.com" Nothing >>= (print :: Either CurlCode String -> IO ())

@Rufflewind
Copy link
Member

Why would the file be created asynchronously?

I don't know. Something odd about the curl library. Perhaps unsafeInterleaveIO? The problem couldn't be reproduced unless I forced the result to NF with fmap length.

Polling the existence of the file at 1 sec intervals:

withTemp: does /tmp/CookieYcViim exist?  True
withTemp: file /tmp/CookieYcViim has been removed
main: does /tmp/CookieYcViim exist?  False
main: curl returned (truncated):
  Right "<!doctype html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en\""
  Right 52318
main: does /tmp/CookieYcViim exist?  False
main: does /tmp/CookieYcViim exist?  True
main: does /tmp/CookieYcViim exist?  True
main: does /tmp/CookieYcViim exist?  True

Here's the code I used:

import Control.Concurrent
import Control.Monad
import Network.Curl
import System.Posix.Temp
import System.Directory
import System.IO
import System.FilePath
import Control.Exception

type PostField = (String, String)

withTemp :: String -> (FilePath -> IO a) -> IO a
withTemp prefix f = do
  tmp <- getTemporaryDirectory
  (fp, h) <- mkstemp $ tmp </> prefix
  hClose h
  finally (f fp) $ do
    putStr ("withTemp: does " ++ fp ++ " exist?  ")
    print =<< doesFileExist fp
    threadDelay 1000000
    removeFile fp
    putStrLn ("withTemp: file " ++ fp ++ " has been removed")

curl :: URLString -> Maybe [PostField] -> IO (FilePath, Either CurlCode String)
curl url values = do
  let mPf = fmap (map (\(k, v) -> k ++ "=" ++ v)) values
  (tmp, (cc, ty)) <- withTemp "Cookie" $ \tmp -> fmap (\x -> (tmp, x)) $ withCurlDo $
    let curlOpts_ = [ CurlHttpAuth [HttpAuthAny]
                    , CurlUserPwd ":"
                    , CurlCookieFile tmp
                    , CurlCookieJar tmp
                    , CurlFollowLocation True
                    , CurlUnrestrictedAuth True ]
        curlOpts = case mPf of
                     Nothing -> curlOpts_
                     Just pf -> (CurlPostFields pf) : curlOpts_
    in curlGetString_ url curlOpts
  return $ (,) tmp $ if cc == CurlOK
              then Right ty
              else Left cc

main :: IO ()
main = do
  (path, result) <- curl "http://google.com" Nothing

  putStr ("main: does " ++ path ++ " exist?  ")
  print =<< doesFileExist path
  threadDelay 1000000

  putStr "main: curl returned (truncated):\n  "
  print (take 80 `fmap` result)
  putStr "  "
  print (length `fmap` result) -- must force result to NF completely!

  forever $ do
    putStr ("main: does " ++ path ++ " exist?  ")
    print =<< doesFileExist path
    threadDelay 1000000

Marking as invalid since the problem is not due to directory.

@Rufflewind Rufflewind added type: x-intended-behavior The described behavior is working as intended. and removed type: a-bug The described behavior is not working as intended. labels Feb 18, 2015
@argiopetech
Copy link
Contributor

Seeing as how this is a "can't fix, won't fix", is there a compelling reason to keep it open?

@Rufflewind
Copy link
Member

I left it open to see if anyone else has any comments.

@UnkindPartition
Copy link

@Rufflewind thanks for the test case, but again, I can't reproduce it. On my system, it never gets to True for the second time.

@Rufflewind
Copy link
Member

@feuerbach I'm using a slightly different version of GHC (7.8.4) but I doubt that's the source of the problem. Mind if I ask what version of libcurl and curl you use? I use curl-1.3.8 and libcurl-7.40.0.

Edit: apparently the version number on libcurl.so is wrong.

@UnkindPartition
Copy link

curl 1.3.8.1, libcurl 7.37.0

@Rufflewind
Copy link
Member

Hrm, I'm not sure what the problem is. I built and linked against curl-7.37.0 and the test case still works for me. Furthermore, curl-1.3.8.1 looks virtually identical to curl-1.3.8 modulo some documentation changes and a version bump.

bgamari pushed a commit to bgamari/directory that referenced this issue Jul 29, 2016
bgamari pushed a commit to bgamari/directory that referenced this issue Jul 29, 2016
bgamari pushed a commit to bgamari/directory that referenced this issue Jul 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: x-intended-behavior The described behavior is working as intended.
Projects
None yet
Development

No branches or pull requests

5 participants