Skip to content

Commit ecf08ce

Browse files
committed
improve task_done_hook code for trimming
1 parent 920c936 commit ecf08ce

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

base/task.jl

+12-9
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,14 @@ macro sync_add(expr)
813813
end
814814
end
815815

816+
throwto_repl_task(@nospecialize val) = throwto(getfield(active_repl_backend, :backend_task)::Task, val)
817+
818+
function is_repl_running()
819+
return isdefined(Base, :active_repl_backend) &&
820+
(getfield(active_repl_backend, :backend_task)::Task)._state === task_state_runnable &&
821+
getfield(active_repl_backend, :in_eval)
822+
end
823+
816824
# runtime system hook called when a task finishes
817825
function task_done_hook(t::Task)
818826
# `finish_task` sets `sigatomic` before entering this function
@@ -834,10 +842,8 @@ function task_done_hook(t::Task)
834842
end
835843

836844
if err && !handled && Threads.threadid() == 1
837-
if isa(result, InterruptException) && active_repl_backend !== nothing &&
838-
active_repl_backend.backend_task._state === task_state_runnable && isempty(Workqueue) &&
839-
active_repl_backend.in_eval
840-
throwto(active_repl_backend.backend_task, result) # this terminates the task
845+
if isa(result, InterruptException) && isempty(Workqueue) && is_repl_running()
846+
throwto_repl_task(result)
841847
end
842848
end
843849
# Clear sigatomic before waiting
@@ -848,11 +854,8 @@ function task_done_hook(t::Task)
848854
# If an InterruptException happens while blocked in the event loop, try handing
849855
# the exception to the REPL task since the current task is done.
850856
# issue #19467
851-
if Threads.threadid() == 1 &&
852-
isa(e, InterruptException) && active_repl_backend !== nothing &&
853-
active_repl_backend.backend_task._state === task_state_runnable && isempty(Workqueue) &&
854-
active_repl_backend.in_eval
855-
throwto(active_repl_backend.backend_task, e)
857+
if Threads.threadid() == 1 && isa(e, InterruptException) && isempty(Workqueue) && is_repl_running()
858+
throwto_repl_task(e)
856859
else
857860
rethrow()
858861
end

0 commit comments

Comments
 (0)