Skip to content

Commit 4aa1df3

Browse files
authored
Merge pull request #35 from TuringLang/exc-info
Extract stacktrace from the failed task
2 parents f59baf5 + 9ae642a commit 4aa1df3

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/taskcopy.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ proper way is refreshing the `current_task` (the variable `t`) in
4040
function task_wrapper(func)
4141
() ->
4242
try
43-
res = func()
4443
ct = current_task()
44+
res = func()
4545
ct.result = res
4646
isa(ct.storage, Nothing) && (ct.storage = IdDict())
4747
ct.storage[:_libtask_state] = :done
@@ -83,6 +83,20 @@ function Base.copy(t::Task)
8383
newt
8484
end
8585

86+
struct CTaskException
87+
etype
88+
msg::String
89+
backtrace::Vector{Union{Ptr{Nothing}, Base.InterpreterIP}}
90+
end
91+
92+
function Base.show(io::IO, exc::CTaskException)
93+
println(io, "Stacktrace in the failed task:\n")
94+
println(io, exc.msg * "\n")
95+
for line in stacktrace(exc.backtrace)
96+
println(io, string(line))
97+
end
98+
end
99+
86100
produce(v) = begin
87101
ct = current_task()
88102

@@ -178,7 +192,12 @@ consume(p::Task, values...) = begin
178192
return p.result
179193
end
180194
if p.exception != nothing
181-
throw(p.exception)
195+
msg = if :msg in fieldnames(typeof(p.exception))
196+
p.exception.msg
197+
else
198+
string(typeof(p.exception))
199+
end
200+
throw(CTaskException(typeof(p.exception), msg, p.backtrace))
182201
end
183202
end
184203
wait()

test/brokentask.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ r = @testset "Broken Functions Tests" begin
1717
try
1818
consume(t)
1919
catch ex
20-
@test isa(ex, ErrorException)
20+
@test ex.etype == ErrorException
2121
end
2222
@test isa(t.exception, ErrorException)
2323
end
@@ -37,7 +37,7 @@ r = @testset "Broken Functions Tests" begin
3737
try
3838
consume(t)
3939
catch ex
40-
@test isa(ex, BoundsError)
40+
@test ex.etype == BoundsError
4141
end
4242
@test isa(t.exception, BoundsError)
4343
end
@@ -58,7 +58,7 @@ r = @testset "Broken Functions Tests" begin
5858
try
5959
consume(t)
6060
catch ex
61-
@test isa(ex, BoundsError)
61+
@test ex.etype == BoundsError
6262
end
6363
@test isa(t.exception, BoundsError)
6464
end
@@ -80,7 +80,7 @@ r = @testset "Broken Functions Tests" begin
8080
try
8181
consume(t_copy)
8282
catch ex
83-
@test isa(ex, BoundsError)
83+
@test ex.etype == BoundsError
8484
end
8585
@test isa(t_copy.exception, BoundsError)
8686
end

test/clonetask.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ function g_break()
5656
end
5757

5858
t = CTask(g_break)
59-
@test_throws MethodError consume(t)
59+
@test_throws Libtask.CTaskException consume(t)

0 commit comments

Comments
 (0)