Skip to content

Commit

Permalink
Merge pull request #35 from TuringLang/exc-info
Browse files Browse the repository at this point in the history
Extract stacktrace from the failed task
  • Loading branch information
yebai authored Jun 3, 2019
2 parents f59baf5 + 9ae642a commit 4aa1df3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
23 changes: 21 additions & 2 deletions src/taskcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ proper way is refreshing the `current_task` (the variable `t`) in
function task_wrapper(func)
() ->
try
res = func()
ct = current_task()
res = func()
ct.result = res
isa(ct.storage, Nothing) && (ct.storage = IdDict())
ct.storage[:_libtask_state] = :done
Expand Down Expand Up @@ -83,6 +83,20 @@ function Base.copy(t::Task)
newt
end

struct CTaskException
etype
msg::String
backtrace::Vector{Union{Ptr{Nothing}, Base.InterpreterIP}}
end

function Base.show(io::IO, exc::CTaskException)
println(io, "Stacktrace in the failed task:\n")
println(io, exc.msg * "\n")
for line in stacktrace(exc.backtrace)
println(io, string(line))
end
end

produce(v) = begin
ct = current_task()

Expand Down Expand Up @@ -178,7 +192,12 @@ consume(p::Task, values...) = begin
return p.result
end
if p.exception != nothing
throw(p.exception)
msg = if :msg in fieldnames(typeof(p.exception))
p.exception.msg
else
string(typeof(p.exception))
end
throw(CTaskException(typeof(p.exception), msg, p.backtrace))
end
end
wait()
Expand Down
8 changes: 4 additions & 4 deletions test/brokentask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ r = @testset "Broken Functions Tests" begin
try
consume(t)
catch ex
@test isa(ex, ErrorException)
@test ex.etype == ErrorException
end
@test isa(t.exception, ErrorException)
end
Expand All @@ -37,7 +37,7 @@ r = @testset "Broken Functions Tests" begin
try
consume(t)
catch ex
@test isa(ex, BoundsError)
@test ex.etype == BoundsError
end
@test isa(t.exception, BoundsError)
end
Expand All @@ -58,7 +58,7 @@ r = @testset "Broken Functions Tests" begin
try
consume(t)
catch ex
@test isa(ex, BoundsError)
@test ex.etype == BoundsError
end
@test isa(t.exception, BoundsError)
end
Expand All @@ -80,7 +80,7 @@ r = @testset "Broken Functions Tests" begin
try
consume(t_copy)
catch ex
@test isa(ex, BoundsError)
@test ex.etype == BoundsError
end
@test isa(t_copy.exception, BoundsError)
end
Expand Down
2 changes: 1 addition & 1 deletion test/clonetask.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ function g_break()
end

t = CTask(g_break)
@test_throws MethodError consume(t)
@test_throws Libtask.CTaskException consume(t)

0 comments on commit 4aa1df3

Please sign in to comment.