SimonDanisch / Bonito.jl

Serving JS to the browser
MIT License
204 stars 30 forks source link

Segfault when doing keyboard interrupt in REPL #191

Open kdheepak opened 10 months ago

kdheepak commented 10 months ago

I have a Package with the following code:

module PackageFoo
using Observables
using Observables: to_value, AbstractObservable
using JSServe
using JSServe: evaljs, onjs, onload, Table, Server
import Tables
import JSServe: jsrender
using JSServe, WGLMakie
using GeometryBasics
using FileIO
using JSServe: @js_str, onjs, App, Slider
using JSServe.DOM
import JSServe.TailwindDashboard as D

const SERVER = Ref{Any}(nothing)

function main(; port::Int=4269, hold=true, verbose=false)
  SERVER[] isa Server && close(SERVER[])

  app = App() do session::Session
    s1 = Slider(1:100)
    s2 = Slider(1:100)
    b = Button("hi")
    t = TextField("enter your text")
    s_value = s1.value
    linkjs(session, s1.value, s2.value)
    test = [1, 2, 3]
    onjs(session, s1.value, js"(v)=> console.log(v + ' ' + $(test))")
    on(t) do text
      println(text)
    end
    return DOM.div(s1, s2, b, t)
  end

  SERVER[] = Server(app, "127.0.0.1", port; verbose)
  # hold && browser("http://localhost:$port")
  hold && wait(SERVER[])
end

If I add an examples/serve.jl with the following code:

using PackageFoo
PackageFoo.main()

And run it and Ctrl + C, I don't get any error.

However, if I run it from the command line like

julia --project -e "using PackageFoo; PackageFoo.main()"

or if I open a julia REPL and run the using PackageFoo; PackageFoo.main(); then it still runs fine however when I hit Ctrl + C I get a segfault sometimes and I get an error other times.

Here's the error:

$ julia -e "using PackageFoo; PackageFoo.main()"
[ Info: Listening on: 127.0.0.1:4269, thread id: 1
[ Info: Server on 127.0.0.1:4269 closing
ERROR: TaskFailedException
Stacktrace:
 [1] wait(t::Task)
   @ Base .\task.jl:349
 [2] wait(server::JSServe.HTTPServer.Server)
   @ JSServe.HTTPServer C:\Users\USERNAME\.julia\packages\JSServe\h2asi\src\HTTPServer\implementation.jl:302
 [3] main(; port::Int64, hold::Bool, verbose::Bool)
   @ PackageFoo C:\Users\USERNAME\gitrepos\PackageFoo\src\PackageFoo.jl:75
 [4] main()
   @ PackageFoo C:\Users\USERNAME\gitrepos\PackageFoo\src\PackageFoo.jl:48
 [5] top-level scope
   @ none:1

    nested task error: InterruptException:
    Stacktrace:
     [1] poptask(W::Base.IntrusiveLinkedListSynchronized{Task})
       @ Base .\task.jl:974
     [2] wait()
       @ Base .\task.jl:983
     [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] wait
       @ C:\Users\USERNAME\.julia\packages\HTTP\SN7VW\src\Servers.jl:125 [inlined]
     [8] macro expansion
       @ C:\Users\USERNAME\.julia\packages\JSServe\h2asi\src\HTTPServer\implementation.jl:287 [inlined]
     [9] (::JSServe.HTTPServer.var"#28#30"{Bool, JSServe.HTTPServer.Server, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}})()
       @ JSServe.HTTPServer .\task.jl:514

I'm not able to reproduce the segfault at the moment but I can update this issue with the segfault stacktrace when I do.

Is this expected?

SimonDanisch commented 10 months ago

I relatively sure that this is a HTTP/Julia issue. Could be wrong, but the stacktrace certainly points to it. You can call close(server) so maybe you can do some different wait loop than wait(SERVER)?