JuliaMath / JuliaMath/Roots.jl

Task not abortable

Open
#467 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
413
Forks
61
Avg merge
57m
Merged PRs (30d)
5

Description

When I define a task that calulates `fzeros()` the task cannot not neither referenced nor aborted until the task finishes.

```julia
using Dates

using Roots
f(x) = exp(-x^2) - sin(x)*cos(x)*x^2

function interrupt(tsk::Task)
@info Dates.seconds(now() - t0), "Interrupting: $(tsk)"
istaskdone(tsk) || Base.schedule(tsk, InterruptException(), error = true)
end

function timeout(f, args...; delay, timed_out = "Timed out", kwargs...)
tsk = @task f(args...; kwargs...)
global t0 = now()
@info Dates.seconds(now() - t0), "timer defined: $(tsk)"
Timer(_ -> interrupt(tsk), delay)
schedule(tsk)
@info Dates.seconds(now() - t0), "task started 1: $(tsk)"
@info Dates.seconds(now() - t0), "task started 2: $(tsk)"
try
result = fetch(tsk)
@info Dates.seconds(now() - t0), "task finished"
result
catch _
timed_out
end
end
```
The resulting output is
```julia-repl
julia> timeout(find_zeros, f, -1e8, 1e8; delay = 0.1, timed_out = "Timed out")
[ Info: (0.0, "timer defined: Task (runnable) @0x00000277d37fc200")
[ Info: (0.0, "task started 1: Task (runnable) @0x00000277d37fc200")
[ Info: (0.378, "task started 2: Task (done) @0x00000277d37fc200")
[ Info: (0.379, "Interrupting: Task (done) @0x00000277d37fc200")
[ Info: (0.38, "task finished")
18635-element Vector{Float64}:
-9.999966976087256e7

9.999966976087256e7

# But this works as expected
julia> timeout(sleep, 1; delay = 0.1, timed_out = "Timed out")
[ Info: (0.0, "timer defined: Task (runnable) @0x00000277d37ff080")
[ Info: (0.002, "task started 1: Task (runnable) @0x00000277d37ff080")
[ Info: (0.003, "task started 2: Task (runnable, started) @0x00000277d37ff080")
[ Info: (0.106, "Interrupting: Task (runnable, started) @0x00000277d37ff080")
"Timed out"
```
This issue relates to https://github.com/GenieFramework/Stipple.jl/issues/282

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.