Skip to content

Commit a417ae9

Browse files
authored
Finally properly address the annoying HTTP broken pipe stuff (#171)
* IOExtras and other tweaks * removing debugging stuff * patch release
1 parent fedf7c8 commit a417ae9

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
name = "LiveServer"
22
uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589"
33
authors = ["Jonas Asprion <jonas.asprion@gmx.ch", "Thibaut Lienart <tlienart@me.com>"]
4-
version = "1.2.4"
4+
version = "1.2.5"
55

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

1314
[compat]
1415
HTTP = "1"
16+
LoggingExtras = "1"
1517
MIMEs = "0.1"
1618
julia = "1.6"

src/LiveServer.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module LiveServer
22

33
import Sockets, Pkg, MIMEs
44
using Base.Filesystem
5-
using Test: TestLogger
65
using Base.Threads: @spawn
6+
using LoggingExtras
77

88
using HTTP
99

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

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

2222
"""Whether to display debug messages while serving"""
23-
const DEBUG = Ref{Bool}(false)
23+
const DEBUG = Ref(false)
2424

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

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

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

3434

3535
set_content_dir(d::String) = (CONTENT_DIR[] = d;)

src/server.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function update_and_close_viewers!(
5252
@sync for wsᵢ in ws_to_update_and_close
5353
isopen(wsᵢ.io) && @spawn begin
5454
try
55+
redirect_stderr()
5556
HTTP.WebSockets.send(wsᵢ, "update")
5657
catch
5758
end
@@ -63,6 +64,7 @@ function update_and_close_viewers!(
6364
@sync for wsi in ws_to_update_and_close
6465
isopen(wsi.io) && @spawn begin
6566
try
67+
redirect_stderr()
6668
wsi.writeclosed = wsi.readclosed = true
6769
close(wsi.io)
6870
catch
@@ -616,6 +618,15 @@ current directory. (See also [`example`](@ref) for an example folder).
616618
)
617619
end
618620

621+
old_logger = global_logger()
622+
old_stderr = stderr
623+
global_logger(
624+
EarlyFilteredLogger(
625+
log -> log._module !== HTTP.Servers,
626+
global_logger()
627+
)
628+
)
629+
619630
server, port = get_server(host, port, req_handler)
620631
host_str = ifelse(host == string(Sockets.localhost), "localhost", host)
621632
url = "http://$host_str:$port"
@@ -624,6 +635,7 @@ current directory. (See also [`example`](@ref) for an example folder).
624635
)
625636

626637
launch_browser && open_in_default_browser(url)
638+
627639
# wait until user interrupts the LiveServer (using CTRL+C).
628640
try
629641
counter = 1
@@ -673,6 +685,11 @@ current directory. (See also [`example`](@ref) for an example folder).
673685
reset_ws_interrupt()
674686
println("")
675687
end
688+
# given that LiveServer is interrupted via an InterruptException, we have
689+
# to be extra careful that things are back as they were before, otherwise
690+
# there's a high risk of the disgusting broken pipe error...
691+
redirect_stderr(old_stderr)
692+
global_logger(old_logger)
676693
return nothing
677694
end
678695

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

0 commit comments

Comments
 (0)