PlotlyJS 0.18.11 hangs when trying "using" - but only the first time!

I am experiencing a strange behavior with PlotlyJS 0.18.11.
The first time I try “using” the package, Julia hangs. If I interrupt the process (CTRL-C, I am on Windows 10) I get error different error messages in VSCode or in the REPL launched from a command prompt (see below). But in both cases, after interrupting the process, a second attempt to “using” works, and I haven’t noticed any problems with plots. I am on Julia 1.9.3, Windows 10, and just downgrading to PlotlyJS 0.18.10 fixes the issue in the environments I tried.
The error message in the REPL seems similar to what has been reported in another thread, but I didn’t find any useful answer there: Cannot use PlotlyJS

PS. Error messages

In the REPL (launched from command prompt)

┌ Warning: Kaleido is not available on this system. Julia will be unable to save images of any plots.
└ @ PlotlyJS C:\Users\UserName\.julia\packages\PlotlyJS\qhYQ5\src\kaleido.jl:65
┌ Warning: InterruptException()
└ @ PlotlyJS C:\Users\UserName\.julia\packages\PlotlyJS\qhYQ5\src\kaleido.jl:66

In VSCode

ERROR: InitError: InterruptException:
  [1] try_yieldto(undo::typeof(Base.ensure_rescheduled))
    @ Base .\task.jl:920
  [2] wait()
    @ Base .\task.jl:984
  [3] wait(c::Base.GenericCondition{Base.Threads.SpinLock}; first::Bool)
    @ Base .\condition.jl:130
  [4] wait(c::Base.GenericCondition{Base.Threads.SpinLock})
    @ Base .\condition.jl:125
  [5] _wait(t::Task)
    @ Base .\task.jl:308
  [6] wait
    @ .\task.jl:347 [inlined]
  [7] __init__()
    @ PlotlyJS C:\Users\UserName\.julia\packages\PlotlyJS\qhYQ5\src\PlotlyJS.jl:167
  [8] register_restored_modules(sv::Core.SimpleVector, pkg::Base.PkgId, path::String)
    @ Base .\loading.jl:1115
  [9] _include_from_serialized(pkg::Base.PkgId, path::String, ocachepath::String, depmods::Vector{Any})
    @ Base .\loading.jl:1061
 [10] _require_search_from_serialized(pkg::Base.PkgId, sourcepath::String, build_id::UInt128)
    @ Base .\loading.jl:1506
 [11] _require(pkg::Base.PkgId, env::String)
    @ Base .\loading.jl:1783
 [12] _require_prelocked(uuidkey::Base.PkgId, env::String)
    @ Base .\loading.jl:1660
 [13] macro expansion
    @ .\loading.jl:1648 [inlined]
 [14] macro expansion
    @ .\lock.jl:267 [inlined]
 [15] require(into::Module, mod::Symbol)
    @ Base .\loading.jl:1611
 [16] eval
    @ .\boot.jl:370 [inlined]
 [17] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
    @ Base .\loading.jl:1903
 [18] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\essentials.jl:819
 [19] invokelatest(::Any, ::Any, ::Vararg{Any})
    @ Base .\essentials.jl:816
 [20] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:263
 [21] (::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:181
 [22] withpath(f::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\repl.jl:274
 [23] (::VSCodeServer.var"#66#71"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:179
 [24] hideprompt(f::VSCodeServer.var"#66#71"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\repl.jl:38
 [25] (::VSCodeServer.var"#65#70"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:150
 [26] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging .\logging.jl:514
 [27] with_logger
    @ .\logging.jl:626 [inlined]
 [28] (::VSCodeServer.var"#64#69"{VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:255
 [29] #invokelatest#2
    @ .\essentials.jl:819 [inlined]
 [30] invokelatest(::Any)
    @ Base .\essentials.jl:816
 [31] macro expansion
    @ c:\Users\UserName\.vscode\extensions\julialang.language-julia-1.56.2\scripts\packages\VSCodeServer\src\eval.jl:34 [inlined]
 [32] (::VSCodeServer.var"#62#63")()
    @ VSCodeServer .\task.jl:514
during initialization of module PlotlyJS

The warning/error is a generic one that results from interrupting the kaleido process startup and initialisation.

To isolate the error, can you try starting the process independently of package loading?

julia> import PlotlyJS

julia> kaleido_task = Base.Threads.@spawn PlotlyJS._start_kaleido_process()

What do you get from the last command? Does it hang?
I get something like

julia>  kaleido_task = Base.Threads.@spawn PlotlyJS._start_kaleido_process()
Task (runnable) @0x000001caefd13080

Then try

julia> wait(kaleido_task)

…if you had otherwise engaged threads you might encounter the long/indefinite wait…

Thank you for your answer.

Julia hangs at the first command (import PlotlyJS)
And if I interrupt it, I get the kaleido warnings (in REPL outside of VSCode) or the same errors as in my original message (in VSCode).

After that, the behavior is different in VSCode and outside of VSCode.

Outside of VSCode, I am able to run the second command (kaleido_task = ...) and I get a Task (runnable) like you. I can also plot - so it is as if the import (or using) command actually worked, but just hanged at the end.

Within VSCode, the second command errors (Task (failed)), but (as before) I can immediately re-run import PlotlyJS or using PlotlyJS and run the kaleido command or plot.

Finally, wait(kaleido_task) seems to hang all the time.

Does your “engaged threads” comment mean I should try restarting Windows? I have obviously restarted Julia many times, but not Windows so far…

I restarted Windows to see if it would help.

It sort of does… After restarting Windows, PlotlyJS seems to be working completely fine in REPL launched from the command prompt. At least most of the time, because sometimes it does hang as before. I know “sometimes” is not helpful, but I couldn’t find a reproducible pattern.

At least, in VSCode, the behavior is reproducible. Same symptoms as before, using or importing PlotlyJS hangs, but if I interrupt the process (CTRL-C) I can re-run the command immediately after and everything seems to be working fine.

I had recently changed my VSCode settings to use multiple threads and to plot outside of VSCode. I thought that it might be related, so I reverted to the default (Settings say “julia.NumThreads”: 0, and the “use plot pane” box is checked). I restarted Windows after making those changes, but it made no difference.

Yes, I was wondering about whether some other thing is stopping the spawned kaleido process to start, but I think it’s not going to be helped by restarting Windows.

You can check how many threads your julia session has available with Threads.nthreads() and see if it matches your expectation?

Ah, just realised this threading code was introduced in version 0.18.11 (Fix precompilation of packages depending on PlotlyJS by Liozou · Pull Request #469 · JuliaPlots/PlotlyJS.jl · GitHub) . You’re probably fine to just stay on PlotlyJS 0.18.10 until a related change in Julia (e.g. atexit: move hook before precompile output by vtjnash · Pull Request #51849 · JuliaLang/julia · GitHub) works it’s way through the code base.

Edit: created an issue to track this:
Kaleido process in package initialisation hangs · Issue #473 · JuliaPlots/PlotlyJS.jl · GitHub

Thank you for your help and for creating the issue. I will revert to 0.18.10 for now.
And just to clarify, the issue is completely reproducible for me within VSCode - but not outside of VSCode (where it does happen, but not always).

Thanks. It’s useful to know it is reproducible on your computer, it’s just hard to reproduce on other similar systems.

Since Kaleido is used for saving static images of plotly interactive plots, it shouldn’t affect just using interactive plotting if that’s all you need. I’m wondering if you can try saving a plot to an image format using savefig() from VSCode after seeing the error/warning? Does it still work?

Saving to HTML or PNG works, saving to SVG hangs.

That’s interesting.

What do you get if you run this after doing savefig()?

julia> PlotlyJS._kaleido_running()