diff --git a/docs/src/man/execution.md b/docs/src/man/execution.md index 04800df0..fe7857a4 100644 --- a/docs/src/man/execution.md +++ b/docs/src/man/execution.md @@ -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 diff --git a/src/launch.jl b/src/launch.jl index 097d5766..a3cf0ef3 100644 --- a/src/launch.jl +++ b/src/launch.jl @@ -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) @@ -270,7 +289,7 @@ 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 @@ -278,6 +297,9 @@ function run_and_collect(cmd; timeout=compiler_timeout) 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)) diff --git a/test/host/launch.jl b/test/host/launch.jl index f74c8f23..cea83273 100644 --- a/test/host/launch.jl +++ b/test/host/launch.jl @@ -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