diff --git a/src/runner/PlutoRunner/src/PlutoRunner.jl b/src/runner/PlutoRunner/src/PlutoRunner.jl index 37d6184a0..46f33c607 100644 --- a/src/runner/PlutoRunner/src/PlutoRunner.jl +++ b/src/runner/PlutoRunner/src/PlutoRunner.jl @@ -76,4 +76,8 @@ include("./io/logging.jl") include("./io/stdout.jl") include("./precompile.jl") +function __init__() + original_stderr[] = stderr +end + end diff --git a/src/runner/PlutoRunner/src/evaluation/deleting globals.jl b/src/runner/PlutoRunner/src/evaluation/deleting globals.jl index b918d80fe..94881905f 100644 --- a/src/runner/PlutoRunner/src/evaluation/deleting globals.jl +++ b/src/runner/PlutoRunner/src/evaluation/deleting globals.jl @@ -89,7 +89,7 @@ function move_vars( catch ex if !(ex isa UndefVarError) @warn "Failed to move variable $(symbol) to new workspace:" - showerror(original_stderr, ex, stacktrace(catch_backtrace())) + showerror(original_stderr[], ex, stacktrace(catch_backtrace())) end end end @@ -198,7 +198,7 @@ function try_delete_toplevel_methods(workspace::Module, (cell_id, name_parts)::T (val isa Function) && delete_toplevel_methods(val, cell_id) catch ex @warn "Failed to delete methods for $(name_parts)" - showerror(original_stderr, ex, stacktrace(catch_backtrace())) + showerror(original_stderr[], ex, stacktrace(catch_backtrace())) false end catch diff --git a/src/runner/PlutoRunner/src/io/logging.jl b/src/runner/PlutoRunner/src/io/logging.jl index e1d3d0507..6231bd5c8 100644 --- a/src/runner/PlutoRunner/src/io/logging.jl +++ b/src/runner/PlutoRunner/src/io/logging.jl @@ -1,7 +1,6 @@ import Logging -const original_stdout = stdout -const original_stderr = stderr +const original_stderr = Ref{IO}() const old_logger = Ref{Union{Logging.AbstractLogger,Nothing}}(nothing) @@ -113,8 +112,9 @@ function Logging.handle_message(pl::PlutoCellLogger, level, msg, _module, group, yield() catch e - println(original_stderr, "Failed to relay log from PlutoRunner") - showerror(original_stderr, e, stacktrace(catch_backtrace())) + Logging.with_logger(Logging.ConsoleLogger(original_stderr[])) do + @error "Failed to relay log from PlutoRunner" exception=(e, catch_backtrace()) + end nothing end diff --git a/test/Logging.jl b/test/Logging.jl index c34d3f265..ed5dc8777 100644 --- a/test/Logging.jl +++ b/test/Logging.jl @@ -66,6 +66,14 @@ using Pluto.WorkspaceManager: poll "show(stdout, collect(1:500))", # 24 "show(stdout, \"text/plain\", collect(1:500))", # 25 "display(collect(1:500))", # 26 + + "struct StructThatErrorsOnPrinting end", # 27 + """ + Base.print(::IO, ::StructThatErrorsOnPrinting) = error("Can't print this") + """, # 28 + """ + @info "" _id=StructThatErrorsOnPrinting() + """, # 29 ])) @testset "Stdout" begin @@ -201,4 +209,28 @@ using Pluto.WorkspaceManager: poll end cleanup(🍭, notebook) + + @testset "Logging error fallback" begin + # This testset needs to use a local worker to capture the worker stderr (which is + # different from the notebook stderr) + 🍍 = ServerSession() + 🍍.options.evaluation.workspace_use_distributed = false + + io = IOBuffer() + old_stderr = PlutoRunner.original_stderr[] + PlutoRunner.original_stderr[] = io + + update_run!(🍍, notebook, notebook.cells[27:29]) + + msg = String(take!(io)) + close(io) + PlutoRunner.original_stderr[] = old_stderr + + @test notebook.cells[27] |> noerror + @test notebook.cells[28] |> noerror + @test notebook.cells[29] |> noerror + @test occursin("Failed to relay log from PlutoRunner", msg) + + cleanup(🍍, notebook) + end end