Skip to content

Commit 0e26ca6

Browse files
authored
Kill the compiler's process group on timeout (#344)
`tileiras` runs `ptxas`. On a timeout, `run_and_collect` killed `tileiras` alone, leaving `ptxas` running unattended for however long the kernel takes it to compile. Outside of Windows the compiler now runs in a session of its own (`detach`), and a timeout kills its process group. Since the compiler then no longer receives the terminal's Ctrl-C, an interrupted wait kills the group too. On Windows, `detach` would take the compiler out of the job object that kills it when Julia exits, so there only the compiler is killed, as before.
1 parent 9fd3a34 commit 0e26ca6

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎docs/src/man/execution.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ disk_cache = true
1515

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

1921
```@docs
2022
cuTile.TileCompilerTimeoutError

‎src/launch.jl‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,28 @@ function discover_tileir_disassembler()
258258
Base.pkgversion(CUDA_Tile_jll))
259259
end
260260

261+
# `tileiras` runs `ptxas`, which a `tileiras` killed alone leaves running. Outside of
262+
# Windows the compiler therefore runs in a session of its own, whose process group
263+
# is killed whole. On Windows, `detach` would instead take the compiler out of the
264+
# job object that kills it when Julia exits, so only the compiler is killed there.
265+
function kill_group(proc)
266+
Sys.iswindows() && return kill(proc, Base.SIGKILL)
267+
# Like `kill(::Process)`, hold the lock so that the process isn't reaped (and
268+
# its pid reused) between the check and the signal.
269+
Base.iolock_begin()
270+
try
271+
process_running(proc) &&
272+
ccall(:kill, Cint, (Cint, Cint), -getpid(proc), Base.SIGKILL)
273+
finally
274+
Base.iolock_end()
275+
end
276+
return
277+
end
278+
261279
function run_and_collect(cmd; timeout=compiler_timeout)
262280
stdout = Pipe()
263-
proc = run(pipeline(ignorestatus(cmd); stdout, stderr=stdout), wait=false)
281+
cmd = Sys.iswindows() ? ignorestatus(cmd) : detach(ignorestatus(cmd))
282+
proc = run(pipeline(cmd; stdout, stderr=stdout), wait=false)
264283
close(stdout.in)
265284
reader = Threads.@spawn String(read(stdout))
266285
timed_out = Ref(false)
@@ -270,14 +289,17 @@ function run_and_collect(cmd; timeout=compiler_timeout)
270289
Timer(timeout) do _
271290
if process_running(proc)
272291
timed_out[] = true
273-
kill(proc, Base.SIGKILL)
292+
kill_group(proc)
274293
end
275294
end
276295
end
277296
try
278297
Base.wait(proc)
279298
finally
280299
timer === nothing || close(timer)
300+
# In its own session, the compiler doesn't see a Ctrl-C, so an interrupted
301+
# wait must not leave it behind.
302+
kill_group(proc)
281303
end
282304
log = strip(fetch(reader))
283305
timed_out[] && throw(TileCompilerTimeoutError(timeout))

‎test/host/launch.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ end
2626
end
2727
@test err isa cuTile.TileCompilerTimeoutError
2828
@test occursin("reducing the tile size", sprint(showerror, err))
29+
30+
# Processes started by the compiler, as `ptxas` is by `tileiras`, are killed
31+
# with it. This one holds the output pipe, so a survivor would keep
32+
# `run_and_collect` waiting.
33+
if !Sys.iswindows()
34+
elapsed = @elapsed err = try
35+
cuTile.run_and_collect(`sh -c 'sleep 60 & wait'`; timeout=0.5)
36+
nothing
37+
catch err
38+
err
39+
end
40+
@test err isa cuTile.TileCompilerTimeoutError
41+
@test elapsed < 30
42+
end
2943
end

0 commit comments

Comments
 (0)