Skip to content

Commit 53e1981

Browse files
authored
Fix fallback for errors in logging (#3013)
1 parent 78fbc95 commit 53e1981

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

β€Žsrc/runner/PlutoRunner/src/PlutoRunner.jl

+4
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ include("./io/logging.jl")
7676
include("./io/stdout.jl")
7777
include("./precompile.jl")
7878

79+
function __init__()
80+
original_stderr[] = stderr
81+
end
82+
7983
end

β€Žsrc/runner/PlutoRunner/src/evaluation/deleting globals.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function move_vars(
8787
catch ex
8888
if !(ex isa UndefVarError)
8989
@warn "Failed to move variable $(symbol) to new workspace:"
90-
showerror(original_stderr, ex, stacktrace(catch_backtrace()))
90+
showerror(original_stderr[], ex, stacktrace(catch_backtrace()))
9191
end
9292
end
9393
end
@@ -198,7 +198,7 @@ function try_delete_toplevel_methods(workspace::Module, (cell_id, name_parts)::T
198198
(val isa Function) && delete_toplevel_methods(val, cell_id)
199199
catch ex
200200
@warn "Failed to delete methods for $(name_parts)"
201-
showerror(original_stderr, ex, stacktrace(catch_backtrace()))
201+
showerror(original_stderr[], ex, stacktrace(catch_backtrace()))
202202
false
203203
end
204204
catch

β€Žsrc/runner/PlutoRunner/src/io/logging.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Logging
22

3-
const original_stdout = stdout
4-
const original_stderr = stderr
3+
const original_stderr = Ref{IO}()
54

65
const old_logger = Ref{Union{Logging.AbstractLogger,Nothing}}(nothing)
76

@@ -113,8 +112,9 @@ function Logging.handle_message(pl::PlutoCellLogger, level, msg, _module, group,
113112
yield()
114113

115114
catch e
116-
println(original_stderr, "Failed to relay log from PlutoRunner")
117-
showerror(original_stderr, e, stacktrace(catch_backtrace()))
115+
Logging.with_logger(Logging.ConsoleLogger(original_stderr[])) do
116+
@error "Failed to relay log from PlutoRunner" exception=(e, catch_backtrace())
117+
end
118118

119119
nothing
120120
end

β€Žtest/Logging.jl

+32
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ using Pluto.WorkspaceManager: poll
6666
"show(stdout, collect(1:500))", # 24
6767
"show(stdout, \"text/plain\", collect(1:500))", # 25
6868
"display(collect(1:500))", # 26
69+
70+
"struct StructThatErrorsOnPrinting end", # 27
71+
"""
72+
Base.print(::IO, ::StructThatErrorsOnPrinting) = error("Can't print this")
73+
""", # 28
74+
"""
75+
@info "" _id=StructThatErrorsOnPrinting()
76+
""", # 29
6977
]))
7078

7179
@testset "Stdout" begin
@@ -201,4 +209,28 @@ using Pluto.WorkspaceManager: poll
201209
end
202210

203211
cleanup(🍭, notebook)
212+
213+
@testset "Logging error fallback" begin
214+
# This testset needs to use a local worker to capture the worker stderr (which is
215+
# different from the notebook stderr)
216+
🍍 = ServerSession()
217+
🍍.options.evaluation.workspace_use_distributed = false
218+
219+
io = IOBuffer()
220+
old_stderr = PlutoRunner.original_stderr[]
221+
PlutoRunner.original_stderr[] = io
222+
223+
update_run!(🍍, notebook, notebook.cells[27:29])
224+
225+
msg = String(take!(io))
226+
close(io)
227+
PlutoRunner.original_stderr[] = old_stderr
228+
229+
@test notebook.cells[27] |> noerror
230+
@test notebook.cells[28] |> noerror
231+
@test notebook.cells[29] |> noerror
232+
@test occursin("Failed to relay log from PlutoRunner", msg)
233+
234+
cleanup(🍍, notebook)
235+
end
204236
end

0 commit comments

Comments
Β (0)