Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/man/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ disk_cache = true

The timeout covers each `tileiras` invocation. On expiry, cuTile terminates the
compiler and reports a [`TileCompilerTimeoutError`](@ref cuTile.TileCompilerTimeoutError).
Except on Windows, the processes the compiler started, such as `ptxas`, are terminated
with it.

```@docs
cuTile.TileCompilerTimeoutError
Expand Down
26 changes: 24 additions & 2 deletions src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,28 @@ function discover_tileir_disassembler()
Base.pkgversion(CUDA_Tile_jll))
end

# `tileiras` runs `ptxas`, which a `tileiras` killed alone leaves running. Outside of
# Windows the compiler therefore runs in a session of its own, whose process group
# is killed whole. On Windows, `detach` would instead take the compiler out of the
# job object that kills it when Julia exits, so only the compiler is killed there.
function kill_group(proc)
Sys.iswindows() && return kill(proc, Base.SIGKILL)
# Like `kill(::Process)`, hold the lock so that the process isn't reaped (and
# its pid reused) between the check and the signal.
Base.iolock_begin()
try
process_running(proc) &&
ccall(:kill, Cint, (Cint, Cint), -getpid(proc), Base.SIGKILL)
finally
Base.iolock_end()
end
return
end

function run_and_collect(cmd; timeout=compiler_timeout)
stdout = Pipe()
proc = run(pipeline(ignorestatus(cmd); stdout, stderr=stdout), wait=false)
cmd = Sys.iswindows() ? ignorestatus(cmd) : detach(ignorestatus(cmd))
proc = run(pipeline(cmd; stdout, stderr=stdout), wait=false)
close(stdout.in)
reader = Threads.@spawn String(read(stdout))
timed_out = Ref(false)
Expand All @@ -270,14 +289,17 @@ function run_and_collect(cmd; timeout=compiler_timeout)
Timer(timeout) do _
if process_running(proc)
timed_out[] = true
kill(proc, Base.SIGKILL)
kill_group(proc)
end
end
end
try
Base.wait(proc)
finally
timer === nothing || close(timer)
# In its own session, the compiler doesn't see a Ctrl-C, so an interrupted
# wait must not leave it behind.
kill_group(proc)
end
log = strip(fetch(reader))
timed_out[] && throw(TileCompilerTimeoutError(timeout))
Expand Down
14 changes: 14 additions & 0 deletions test/host/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ end
end
@test err isa cuTile.TileCompilerTimeoutError
@test occursin("reducing the tile size", sprint(showerror, err))

# Processes started by the compiler, as `ptxas` is by `tileiras`, are killed
# with it. This one holds the output pipe, so a survivor would keep
# `run_and_collect` waiting.
if !Sys.iswindows()
elapsed = @elapsed err = try
cuTile.run_and_collect(`sh -c 'sleep 60 & wait'`; timeout=0.5)
nothing
catch err
err
end
@test err isa cuTile.TileCompilerTimeoutError
@test elapsed < 30
end
end