Skip to content

Commit

Permalink
temp cleanup: on Windows GC before trying to delete files
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Aug 11, 2019
1 parent 4f2b64a commit 5acab85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 15 additions & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,28 @@ function temp_cleanup_later(path::AbstractString; asap::Bool=false)
end

function temp_cleanup_purge()
need_gc = Sys.iswindows()
for (path, asap) in TEMP_CLEANUP
asap && rm(path, recursive=true, force=true)
if asap && ispath(path)
if need_gc
GC.gc()
need_gc = false
end
rm(path, recursive=true, force=true)
end
!ispath(path) && delete!(TEMP_CLEANUP, path)
end
end

function temp_cleanup_atexit()
need_gc = Sys.iswindows()
for path in keys(TEMP_CLEANUP)
if ispath(path)
if need_gc
GC.gc()
need_gc = false
end
end
rm(path, recursive=true, force=true)
end
end
Expand Down
15 changes: 11 additions & 4 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function with_temp_temp_cleanup(f::Function, n::Int)
TEMP_CLEANUP_MAX[] = n
try f()
finally
Sys.iswindows() && GC.gc()
for t in keys(TEMP_CLEANUP)
rm(t, recursive=true, force=true)
end
Expand All @@ -89,14 +90,20 @@ function with_temp_temp_cleanup(f::Function, n::Int)
end
end

function mktempfile(; cleanup=true)
(file, io) = mktemp(cleanup=cleanup)
Sys.iswindows() && close(io)
return file
end

@testset "mktemp/dir cleanup list purging" begin
n = 12 # cleanup min & max
@assert n % 2 == n % 3 == 0 # otherwise tests won't work
with_temp_temp_cleanup(n) do
# for n mktemps, no purging is triggered
temps = String[]
for i = 1:n
t = i % 2 == 0 ? mktemp()[1] : mktempdir()
t = i % 2 == 0 ? mktempfile() : mktempdir()
push!(temps, t)
@test ispath(t)
@test length(TEMP_CLEANUP) ==
Expand All @@ -111,7 +118,7 @@ end
@test TEMP_CLEANUP_MAX[] == n
rm(t, recursive=true, force=true)
# purge triggered by next mktemp with cleanup
t = mktemp()[1]
t = mktempfile()
push!(temps, t)
n′ = 2n÷3 + 1
@test 2n′ > n
Expand All @@ -124,7 +131,7 @@ end
end
# for n′ mktemps, no purging is triggered
for i = 1:n′
t = i % 2 == 0 ? mktemp()[1] : mktempdir()
t = i % 2 == 0 ? mktempfile() : mktempdir()
push!(temps, t)
@test ispath(t)
@test length(TEMP_CLEANUP) == n′ + i
Expand All @@ -133,7 +140,7 @@ end
i % 3 != 0 && rm(t, recursive=true, force=true)
end
# without cleanup no purge is triggered
t = mktemp(cleanup=false)[1]
t = mktempfile(cleanup=false)
@test isfile(t)
@test length(TEMP_CLEANUP) == 2n′
@test TEMP_CLEANUP_MAX[] == 2n′
Expand Down

0 comments on commit 5acab85

Please sign in to comment.