IOError: unlink: resource busy or locked (EBUSY) in Windows

Hi!

I need help. I am trying to copy a file but I am seeing this error only in Windows (only with Julia >= v.1.2.0):

Include Pretty Table to file: Error During Test at C:\projects\prettytables-jl\test\general.jl:12
Got exception outside of a @test
IOError: unlink: resource busy or locked (EBUSY)
Stacktrace:
 [1] uv_error at .\libuv.jl:90 [inlined]
 [2] unlink(::String) at .\file.jl:730
 [3] #rm#9(::Bool, ::Bool, ::typeof(rm), ::String) at .\file.jl:253
 [4] #rm at .\none:0 [inlined]
 [5] #checkfor_mv_cp_cptree#10(::Bool, ::typeof(Base.Filesystem.checkfor_mv_cp_cptree), ::String, ::String, ::String) at .\file.jl:291
 [6] #checkfor_mv_cp_cptree at .\none:0 [inlined]
 [7] #cp#12(::Bool, ::Bool, ::typeof(cp), ::String, ::String) at .\file.jl:330
 [8] #cp at .\none:0 [inlined]
 [9] #include_pt_in_file#44(::Bool, ::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol},NamedTuple{(:tf, :hlines),Tuple{TextFormat,Array{Int32,1}}}}, ::typeof(include_pt_in_file), ::String, ::String, ::Array{Int32,2}) at C:\projects\prettytables-jl\src\files.jl:61
 [10] (::getfield(PrettyTables, Symbol("#kw##include_pt_in_file")))(::NamedTuple{(:tf, :hlines, :backup_file),Tuple{TextFormat,Array{Int32,1},Bool}}, ::typeof(include_pt_in_file), ::String, ::String, ::Array{Int32,2}) at .\none:0
 [11] top-level scope at C:\projects\prettytables-jl\test\general.jl:44
 [12] top-level scope at C:\cygwin\home\Administrator\buildbot\worker\package_win32\build\usr\share\julia\stdlib\v1.2\Test\src\Test.jl:1113
 [13] top-level scope at C:\projects\prettytables-jl\test\general.jl:13
 [14] include at .\boot.jl:328 [inlined]
 [15] include_relative(::Module, ::String) at .\loading.jl:1094
 [16] include(::Module, ::String) at .\Base.jl:31
 [17] include(::String) at .\client.jl:431
 [18] top-level scope at C:\projects\prettytables-jl\test\runtests.jl:20
 [19] include at .\boot.jl:328 [inlined]
 [20] include_relative(::Module, ::String) at .\loading.jl:1094
 [21] include(::Module, ::String) at .\Base.jl:31
 [22] include(::String) at .\client.jl:431
 [23] top-level scope at none:5
 [24] eval(::Module, ::Any) at .\boot.jl:330
 [25] exec_options(::Base.JLOptions) at .\client.jl:271
 [26] _start() at .\client.jl:464

The code is:

function include_pt_in_file(filename::AbstractString, mark::AbstractString,
                            args...; backup_file = true, kwargs...)

    orig = ""

    open(filename, "r") do f
        orig = read(f, String)
    end

    GC.gc()

    # First, print the a string.
    io = IOBuffer()
    pretty_table(io, args...; kwargs...)
    str = String(take!(io))

    # Write the output to a temporary file.
    (path,io) = mktemp()
    r = Regex("(?<=<PrettyTables $mark>)(?:.|\n)*?(?=.*</PrettyTables>)")
    write(io, replace(orig, r => "\n$str"))
    close(io)

    # Backup the original file if required.
    backup_file && mv(filename, filename * "_backup"; force = true)

    # Copy the temporary file to `filename`.
    cp(path, filename; force = true)

    return nothing
end

The error occurs in the line:

    cp(path, filename; force = true)

and I am using backup_file = false.

I found some related posts like:

but I was not able to fix the problem. Can anyone help me?

Found! The problem is that I was calling mktemp in the runtest.jl and then trying to overwrite the file created by this function, leading to the crash.

3 Likes