Skip to content

Commit ec6b7ab

Browse files
Liozouvtjnash
andcommitted
More review comments, add temp_cleanup_postprocess
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
1 parent 253588b commit ec6b7ab

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

base/file.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ end
327327
# Loaded DLLs cannot be deleted on Windows, even with posix delete mode but they can be renamed.
328328
# delayed_delete_dll(path) does so temporarily, until later cleanup by Pkg.gc().
329329
function delayed_delete_dll(path)
330-
drive = first(splitdrive(path))
331-
tmpdrive = first(splitdrive(tempdir()))
332330
# in-use DLL must be kept on the same drive
333331
temp_path = tempname(abspath(dirname(path)); cleanup=false, suffix=string("_", basename(path)))
334332
@debug "Could not delete DLL most likely because it is loaded, moving to a temporary path" path temp_path
@@ -687,8 +685,41 @@ end
687685
# deprecated internal function used by some packages
688686
temp_cleanup_purge(; force=false) = force ? temp_cleanup_purge_all() : @lock TEMP_CLEANUP_LOCK temp_cleanup_purge_prelocked(false)
689687

688+
function temp_cleanup_postprocess(cleanup_dirs)
689+
if !isempty(cleanup_dirs)
690+
rmcmd = """
691+
eof(stdin)
692+
for path in eachline(stdin)
693+
try
694+
rm(path, force=true, recursive=true)
695+
catch ex
696+
@warn "Failed to clean up temporary path \$(repr(path))\n\$ex" _group=:file
697+
end
698+
end
699+
"""
700+
cmd = Cmd(`$(Base.julia_cmd()) --startup-file=no -e $rmcmd`; ignorestatus=true, detach=true)
701+
pw = Base.PipeEndpoint()
702+
rd, wr = Base.link_pipe(true, true)
703+
try
704+
Base.open_pipe!(pw, wr)
705+
catch
706+
Base.close_pipe_sync(wr)
707+
rethrow()
708+
end
709+
run(cmd, rd, devnull, stderr; wait=false)
710+
join(pw, cleanup_dirs, "\n")
711+
Base.dup(wr) # intentionally leak a reference, until the process exits
712+
close(pw)
713+
end
714+
end
715+
716+
function temp_cleanup_atexit()
717+
temp_cleanup_purge_all()
718+
temp_cleanup_postprocess(keys(TEMP_CLEANUP))
719+
end
720+
690721
function __postinit__()
691-
Base.atexit(temp_cleanup_purge_all)
722+
Base.atexit(temp_cleanup_atexit)
692723
end
693724

694725
const temp_prefix = "jl_"

test/tempdepot.jl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@
99

1010
if Sys.iswindows() && !@isdefined(DEPOTS_TOREMOVE)
1111
const DEPOTS_TOREMOVE = String[]
12-
atexit() do # launch a process that will rm the depots
13-
rmcmd = """
14-
for _ in 1:3600 # wait up to 1h from atexit() until the julia testing process dies
15-
sleep(1)
16-
ccall(:uv_kill, Cint, (Cuint, Cint), $(getpid()), 0) == Base.UV_ESRCH && break
17-
end
18-
for path in $DEPOTS_TOREMOVE
19-
try
20-
rm(path, force=true, recursive=true)
21-
catch
22-
end
23-
end
24-
"""
25-
cmd = Cmd(`$(Base.julia_cmd()) --startup-file=no -e $rmcmd`; ignorestatus=true, detach=true)
26-
run(cmd; wait=false)
27-
end
12+
atexit(() -> Base.Filesystem.temp_cleanup_postprocess(DEPOTS_TOREMOVE))
2813
end
2914

3015
function rmdepot(depot)

0 commit comments

Comments
 (0)