List all live(non gc'ed) tasks?

Is there a way to get a list of all “live” tasks in the current session?

I’ve been looking into Julia’s task management code lately. I don’t have the full picture yet but I think this is hard beacuse there isn’t a master list of tasks.

Say I create a Condition and a Task that waits on it:

julia> c = Condition()
julia> t = @async begin
    wait(c)
    println("finished waiting")
end

When I run the @async my new Task is created and added to the Workqueue (a queue of runnable tasks). When my current Task needs to wait for something (e.g. keyboard input at the REPL), it will pop off t and switch to it. When t runs it will call wait(c), adding itself to c’s queue. At this point you could consider t to be a “live” task, but it’s not stored in some global list, the only reference to it (aside from in our REPL session) is within c’s wait queue.

I think that to do this you’d need to do a full heap search starting at the GC roots (like a GC “mark” phase) and find all the Task objects in :runnable, :waiting or :queued states.

I’ve definitely thought it would be useful to have a top-style list of active tasks (and ideally be able to kill them selectively), but that would probably require a step where all Tasks register themselves with some global list on creation.

3 Likes

@ssfrr old topic, but I am running into the same demand today - do you have some tips on how to do the heap search?

Sorry, I don’t think I ever followed this through.

1 Like

I think it would be to use Julia’s builtin profilers together with a visualizer like GitHub - JuliaPerf/PProf.jl: Export Julia profiles to the pprof format (supports allocations). The vscode extension has their own visualization with @profview_allocs.

You could post-procesa the heap snapshot.