Skip to content

File not closed properly after usage on Windows #43673

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

Open
abx78 opened this issue Jan 6, 2022 · 7 comments
Open

File not closed properly after usage on Windows #43673

abx78 opened this issue Jan 6, 2022 · 7 comments
Labels
filesystem Underlying file system and functions that use it needs more info Clarification or a reproducible example is required system:windows Affects only Windows

Comments

@abx78
Copy link

abx78 commented Jan 6, 2022

Even on basic scenarios, a file on windows does not get released correctly by the process

data = open(file_name) do f
    read(f, String)
end

After this, the Computer Management on Windows will show the file in the Open Files for a while.

This can help temporarily fixing the issue

function open(f, file)
    io = open(file)
    handle = Libc._get_osfhandle(RawFD(fd(io)))
    try
        return f(io)
    finally
        close(io)
        ccall(:CloseHandle, stdcall, Cint, (Ptr{Cvoid},), handle)
    end
end

julia> versioninfo()
Julia Version 1.6.4
Commit 35f0c91 (2021-11-19 03:54 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Xeon(R) Gold 6242 CPU @ 2.80GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, skylake-avx512)

@DilumAluthge DilumAluthge added system:windows Affects only Windows bug Indicates an unexpected problem or unintended behavior filesystem Underlying file system and functions that use it labels Jan 6, 2022
@ararslan
Copy link
Member

ararslan commented Jan 6, 2022

@abx78 and I were discussing this a bit in Slack. Interestingly, the call to CloseHandle is necessary for Windows to release the file handle but the return code from the call is 0, which supposedly denotes an error. I wonder whether https://github.com/JuliaLang/julia/blob/master/base/stream.jl#L370 isn't doing the right thing on Windows? I can't figure out where that _close actually comes from. (it's a Windows API function)

@ararslan
Copy link
Member

ararslan commented Jan 7, 2022

cc @vtjnash who may have some insight

@IanButterworth
Copy link
Member

Related issue? #39457

@vtjnash vtjnash removed the bug Indicates an unexpected problem or unintended behavior label Jan 24, 2022
@vtjnash
Copy link
Member

vtjnash commented Jan 24, 2022

I don't see this. When I run the code above, it appears in Computer Management. Then when I close the file and click the refresh button in Computer Management it disappears, as expected.

@vtjnash vtjnash added the needs more info Clarification or a reproducible example is required label Jan 24, 2022
@yummy527
Copy link

OS: windows 10
Julia Version: 1.9.2

When trying to rm a file after open and check the file, I often get the error "resource busy or locked". I suspect that something is wrong with the open or read function, which leading me here.

By the way, the same code does not trigger an error on my Linux computer.

Please fix it on Windows.

@vtjnash
Copy link
Member

vtjnash commented Jul 31, 2023

Yes, posix-compliant kernels don't have this problem; it is unique to the NT kernel implementation. This is often a problem with running virus checkers, so do you happen to have one running?

@abx78
Copy link
Author

abx78 commented Jul 31, 2023

In my case, I did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
filesystem Underlying file system and functions that use it needs more info Clarification or a reproducible example is required system:windows Affects only Windows
Projects
None yet
Development

No branches or pull requests

6 participants