Skip to content

Task not abortable #467

@hhaensel

Description

@hhaensel

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

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> 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 GenieFramework/Stipple.jl#282

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions