Skip to content

Commit

Permalink
Fix fallback for errors in logging (#3013)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwe authored Sep 22, 2024
1 parent 78fbc95 commit 53e1981
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/runner/PlutoRunner/src/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ include("./io/logging.jl")
include("./io/stdout.jl")
include("./precompile.jl")

function __init__()
original_stderr[] = stderr
end

end
4 changes: 2 additions & 2 deletions src/runner/PlutoRunner/src/evaluation/deleting globals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/runner/PlutoRunner/src/io/logging.jl
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/Logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 53e1981

Please sign in to comment.