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

CSV.read leaves file open on Windows 7? #53

Closed
jaakkor2 opened this issue Dec 24, 2016 · 6 comments
Closed

CSV.read leaves file open on Windows 7? #53

jaakkor2 opened this issue Dec 24, 2016 · 6 comments

Comments

@jaakkor2
Copy link

using CSV
CSV.read("data.txt")

looks like to leave the file open in Windows 7. If I try to delete the file data.txt in Windows Explorer, I get the dialog "File In Use; The action can't be completed because the file is open in Julia Programming Language; Close the file and try again". If I close Julia, I can delete the file. However, I can delete the file in Git Bash without closing Julia.

On Mac it seems to work fine.

Versions

  • Julia Version 0.5.0
  • CSV v"0.1.2"
@quinnj
Copy link
Member

quinnj commented Dec 24, 2016

Hmmm.....I'm not sure we can do anything here; we're calling Mmap.mmap from julia Base and I think that's just part of the behavior on windows with mmapping (i.e. you're not allowed to delete a file that is currently mmapped). As long as the CSV.Source is in scope, I think we have to expect this behavior. But if you're just calling CSV.read, you can probably call gc(); gc(); afterwards and then be able to delete the file (this forces a garbage collection on the mmap and should release the file to be deleted).

@jaakkor2
Copy link
Author

Thanks. That workaround works.

CSV.read("data.txt")
if is_windows()
    gc()
    gc()
end

Does not look nice though. If there is no handle of the file given to the user, I think the memory should be unmapped after reading.

@jaakkor2
Copy link
Author

This works as well

a=CSV.read("data.txt"; use_mmap=false)

@bkamins
Copy link
Member

bkamins commented Jan 3, 2017

readdlm in Julia Base by default sets use_mmap to false on Windows by testing is_windows() ? false : true probably because of the problem mentioned in this issue.

Maybe the same approach could be used in definition of Source like:

Source(fullpath::Union{AbstractString,IO};

              delim=COMMA,
              quotechar=QUOTE,
              escapechar=ESCAPE,
              null::AbstractString="",

              header::Union{Integer,UnitRange{Int},Vector}=1, # header can be a row number, range of rows, or actual string vector
              datarow::Int=-1, # by default, data starts immediately after header or start of file
              types::Union{Dict{Int,DataType},Dict{String,DataType},Vector{DataType}}=DataType[],
              nullable::Bool=true,
              weakrefstrings::Bool=true,
              dateformat::Union{AbstractString,Dates.DateFormat}=Dates.ISODateFormat,

              footerskip::Int=0,
              rows_for_type_detect::Int=100,
              rows::Int=0,
              use_mmap::Bool=is_windows() ? false : true)

@quinnj
Copy link
Member

quinnj commented Feb 28, 2017

Here's the tradeoff: mmap is much faster, and this issue only comes up when you read a file and then quickly try to delete the underlying file. I could add some documentation around what needs to happen to safely delete, but it seems worth it to keep the speedups by using mmap by default on windows.

@bkamins
Copy link
Member

bkamins commented Feb 28, 2017

I would find this additional documentation very useful (and maybe the conclusions could be also used in readdlm in Base). Is the problem only related to deletion of the file or also to its modification?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants