Skip to content

Commit

Permalink
Finally properly address the annoying HTTP broken pipe stuff (#171)
Browse files Browse the repository at this point in the history
* IOExtras and other tweaks

* removing debugging stuff

* patch release
  • Loading branch information
tlienart authored Jul 20, 2023
1 parent fedf7c8 commit a417ae9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name = "LiveServer"
uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589"
authors = ["Jonas Asprion <[email protected]", "Thibaut Lienart <[email protected]>"]
version = "1.2.4"
version = "1.2.5"

[deps]
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
MIMEs = "6c6e2e6c-3030-632d-7369-2d6c69616d65"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
HTTP = "1"
LoggingExtras = "1"
MIMEs = "0.1"
julia = "1.6"
10 changes: 5 additions & 5 deletions src/LiveServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module LiveServer

import Sockets, Pkg, MIMEs
using Base.Filesystem
using Test: TestLogger
using Base.Threads: @spawn
using LoggingExtras

using HTTP

Expand All @@ -17,19 +17,19 @@ export serve, servedocs
const BROWSER_RELOAD_SCRIPT = read(joinpath(@__DIR__, "client.html"), String)

"""Whether to display messages while serving or not, see [`verbose`](@ref)."""
const VERBOSE = Ref{Bool}(false)
const VERBOSE = Ref(false)

"""Whether to display debug messages while serving"""
const DEBUG = Ref{Bool}(false)
const DEBUG = Ref(false)

"""The folder to watch, either the current one or a specified one (dir=...)."""
const CONTENT_DIR = Ref{String}("")
const CONTENT_DIR = Ref("")

"""List of files being tracked with WebSocket connections."""
const WS_VIEWERS = Dict{String,Vector{HTTP.WebSockets.WebSocket}}()

"""Keep track of whether an interruption happened while processing a websocket."""
const WS_INTERRUPT = Base.Ref{Bool}(false)
const WS_INTERRUPT = Ref(false)


set_content_dir(d::String) = (CONTENT_DIR[] = d;)
Expand Down
18 changes: 18 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function update_and_close_viewers!(
@sync for wsᵢ in ws_to_update_and_close
isopen(wsᵢ.io) && @spawn begin
try
redirect_stderr()
HTTP.WebSockets.send(wsᵢ, "update")
catch
end
Expand All @@ -63,6 +64,7 @@ function update_and_close_viewers!(
@sync for wsi in ws_to_update_and_close
isopen(wsi.io) && @spawn begin
try
redirect_stderr()
wsi.writeclosed = wsi.readclosed = true
close(wsi.io)
catch
Expand Down Expand Up @@ -616,6 +618,15 @@ current directory. (See also [`example`](@ref) for an example folder).
)
end

old_logger = global_logger()
old_stderr = stderr
global_logger(
EarlyFilteredLogger(
log -> log._module !== HTTP.Servers,
global_logger()
)
)

server, port = get_server(host, port, req_handler)
host_str = ifelse(host == string(Sockets.localhost), "localhost", host)
url = "http://$host_str:$port"
Expand All @@ -624,6 +635,7 @@ current directory. (See also [`example`](@ref) for an example folder).
)

launch_browser && open_in_default_browser(url)

# wait until user interrupts the LiveServer (using CTRL+C).
try
counter = 1
Expand Down Expand Up @@ -673,6 +685,11 @@ current directory. (See also [`example`](@ref) for an example folder).
reset_ws_interrupt()
println("")
end
# given that LiveServer is interrupted via an InterruptException, we have
# to be extra careful that things are back as they were before, otherwise
# there's a high risk of the disgusting broken pipe error...
redirect_stderr(old_stderr)
global_logger(old_logger)
return nothing
end

Expand All @@ -694,6 +711,7 @@ function get_server(
incr >= 10 && @error "couldn't find a free port in $incr tries"
try
server = HTTP.listen!(host, port; readtimeout=0, verbose=-1) do http::HTTP.Stream
redirect_stderr()
if HTTP.WebSockets.isupgrade(http.message)
# upgrade to websocket and add to list of viewers and keep open
# until written to
Expand Down

2 comments on commit a417ae9

@tlienart
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/87924

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.5 -m "<description of version>" a417ae9d829eab3091d813e394f0213f8a06cfbc
git push origin v1.2.5

Please sign in to comment.