Skip to content

Commit fe9a09b

Browse files
committed
handle InterruptException a bit better
1 parent 2a91c13 commit fe9a09b

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

src/ReTest.jl

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ function retest(args::Union{Module,AbstractString,Regex}...;
369369
nprinted = 0
370370
allpass = true
371371
exception = Ref{Exception}()
372+
interrupted = Threads.Atomic{Bool}(false)
372373

373374
module_ts = Testset.ReTestSet("", string(mod) * ':', true)
374375
push!(root.results, module_ts)
@@ -409,13 +410,13 @@ function retest(args::Union{Module,AbstractString,Regex}...;
409410
end
410411

411412
previewer = previewchan === nothing ? nothing :
412-
@async begin
413+
@async try
413414
timer = ['|', '/', '-', '\\']
414415
cursor = 0
415416
desc = ""
416417
finito = false
417418

418-
while !finito
419+
while !finito && !interrupted[]
419420
lock(printlock) do
420421
newdesc = take_latest!(previewchan)
421422
if newdesc === nothing
@@ -465,7 +466,17 @@ function retest(args::Union{Module,AbstractString,Regex}...;
465466
end
466467
sleep(0.13)
467468
end
468-
end
469+
catch ex
470+
# TODO: clarify what is the correct thing to do here
471+
if ex isa InterruptException
472+
interrupted[] = true
473+
rethrow()
474+
else
475+
# then there is probably a bug in the previewer code, but it might be fine
476+
# for the worker/printer to continue?
477+
rethrow()
478+
end
479+
end # previewer task
469480

470481
# TODO: move printer task out of worker?
471482
worker = @task begin
@@ -493,7 +504,7 @@ function retest(args::Union{Module,AbstractString,Regex}...;
493504
align_overflow = 0
494505
end
495506

496-
while !finito
507+
while !finito && !interrupted[]
497508
rts = take!(outchan)
498509
lock(printlock) do
499510
if previewchan !== nothing
@@ -538,7 +549,7 @@ function retest(args::Union{Module,AbstractString,Regex}...;
538549
end
539550
end
540551
end
541-
end
552+
end # printer task
542553

543554
ndone = 0
544555

@@ -553,15 +564,14 @@ function retest(args::Union{Module,AbstractString,Regex}...;
553564
@async put!(computechan, nothing)
554565
end
555566

556-
557567
@sync for wrkr in workers()
558568
@async begin
559569
if nprocs() == 1
560570
take!(computechan)
561571
end
562572
file = nothing
563573
idx = 0
564-
while ndone < length(tests)
574+
while ndone < length(tests) && !interrupted[]
565575
ndone += 1
566576
if !@isdefined(groups)
567577
ts = tests[ndone]
@@ -600,19 +610,12 @@ function retest(args::Union{Module,AbstractString,Regex}...;
600610
put!(previewchan, desc)
601611
end
602612

603-
resp = try
604-
chan = (out=outchan, compute=computechan, preview=previewchan)
605-
remotecall_fetch(wrkr, mod, ts, regex, chan
613+
chan = (out=outchan, compute=computechan, preview=previewchan)
614+
resp = remotecall_fetch(wrkr, mod, ts, regex, chan
606615
) do mod, ts, regex, chan
607616
mts = make_ts(ts, regex, format.stats, chan)
608617
Core.eval(mod, mts)
609618
end
610-
catch e
611-
allpass = false
612-
ndone = length(tests)
613-
isa(e, InterruptException) || rethrow()
614-
return
615-
end
616619
if resp isa Vector
617620
ntests += length(resp)
618621
append!(module_ts.results, resp)
@@ -622,29 +625,38 @@ function retest(args::Union{Module,AbstractString,Regex}...;
622625
end
623626

624627
end
625-
end
626-
end
628+
end # wrkr: @async
629+
end # @sync for wrkr...
627630

631+
# TODO: maybe put the following stuff in a finally clause where we schedule worker
632+
# (as part of the mechanism to handle exceptions vs interrupt[])
628633
put!(outchan, nothing)
629634
previewchan !== nothing &&
630635
put!(previewchan, nothing)
631636
wait(printer)
632-
end
637+
end # worker = @task begin ...
633638

634-
if previewchan !== nothing # then nthreads() > 1
635-
# we try to keep thread #1 free of heavy work, so that the previewer stays
636-
# responsive
637-
tid = rand(2:nthreads())
638-
thread_pin(worker, UInt16(tid))
639-
else
640-
schedule(worker)
641-
end
639+
try
640+
if previewchan !== nothing # then nthreads() > 1
641+
# we try to keep thread #1 free of heavy work, so that the previewer stays
642+
# responsive
643+
tid = rand(2:nthreads())
644+
thread_pin(worker, UInt16(tid))
645+
else
646+
schedule(worker)
647+
end
642648

643-
wait(worker)
644-
previewer !== nothing &&
645-
wait(previewer)
649+
wait(worker)
650+
previewer !== nothing &&
651+
wait(previewer)
652+
653+
catch ex
654+
interrupted[] = true
655+
ex isa InterruptException ||
656+
rethrow()
657+
end
646658

647-
@assert !allpass || nprinted == ntests
659+
@assert interrupted[] || !allpass || nprinted == ntests
648660
if isassigned(exception)
649661
throw(exception[])
650662
end

0 commit comments

Comments
 (0)