Skip to content

Scheduler: do not always switch to the scheduler task #58765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,11 @@ end

@inline function wait_forever()
while true
wait()
try
wait()
catch
# ignore SIGINT and anything thrown from wait()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still kill the application, as currently this would make julia unkillable with ^C, which isn't intended (there is also some logic in task_done_hook to move the exception to the correct Task, which this prevents)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the logic in task_done_hook, and thought about replicating it here but wasn't sure that was the right thing to do.

If we allow an InterruptException to kill this task, and simply restart it from wait(), then how is that different from just ignoring it?

end
end
end

Expand Down Expand Up @@ -1204,11 +1208,13 @@ function wait()
W = workqueue_for(Threads.threadid())
task = trypoptask(W)
if task === nothing
# No tasks to run; switch to the scheduler task to run the
# thread sleep logic.
sched_task = get_sched_task()
if ct !== sched_task
return yieldto(sched_task)
# No tasks to run; if the current task is done/failed, switch to the
# scheduler task to run the thread sleep logic.
if istaskdone(ct)
sched_task = get_sched_task()
if ct !== sched_task
return yieldto(sched_task)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return yieldto(sched_task)
istaskdone(sched_task) && sched_task = reset(get_sched_task)
return yieldto(sched_task)

(plus implement reset)

Or remove the OncePerThread{Task}, since it adds a lot of unnecessary complexity here, and just use @task wait() here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: @task wait(), I feel that it would add avoidable latency. If there is a way to implement a reset, that would make it unnecessary, yes? But how would I implement reset?

Copy link
Member

@vtjnash vtjnash Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@task nothing is quite a bit less overhead than the while loop has now, and just directly switches to the task_done_hook (which ends with wait()) that uses a fresh task. The only complication I foresee is needing to flag that task as being a scheduler task, so that we don't keep looping here. So better yet:

scheduler_task() = nothing
if ct.start !== scheduler_task
     yieldto(Task(scheduler_task))
end

end
end
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any), trypoptask, W, checktaskempty)
end
Expand Down