Skip to content

Commit

Permalink
net.http: fix panic on empty addr, cleanup listen_and_serve Server …
Browse files Browse the repository at this point in the history
…method (#21164)
  • Loading branch information
ttytm committed Apr 2, 2024
1 parent a1b6360 commit be0986d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
20 changes: 5 additions & 15 deletions vlib/net/http/server.v
Expand Up @@ -61,15 +61,10 @@ pub fn (mut s Server) listen_and_serve() {
eprintln('Failed getting listener address, err: ${err}')
return
}
mut listening_address := s.addr.clone()
if l.family() == net.AddrFamily.unspec {
if listening_address == ':0' {
listening_address = 'localhost:0'
}
mut listen_family := net.AddrFamily.ip
// $if !windows {
// listen_family = net.AddrFamily.ip6
// }
listening_address := if s.addr == '' || s.addr == ':0' { 'localhost:0' } else { s.addr }
listen_family := net.AddrFamily.ip
// listen_family := $if windows { net.AddrFamily.ip } $else { net.AddrFamily.ip6 }
s.listener = net.listen_tcp(listen_family, listening_address) or {
eprintln('Listening on ${s.addr} failed, err: ${err}')
return
Expand All @@ -84,7 +79,6 @@ pub fn (mut s Server) listen_and_serve() {

// Create tcp connection channel
ch := chan &net.TcpConn{cap: s.pool_channel_slots}

// Create workers
mut ws := []thread{cap: s.worker_num}
for wid in 0 .. s.worker_num {
Expand All @@ -101,14 +95,10 @@ pub fn (mut s Server) listen_and_serve() {
if s.on_running != unsafe { nil } {
s.on_running(mut s)
}
for {
// break if we have a stop signal
if s.state != .running {
break
}
for s.state == .running {
mut conn := s.listener.accept() or {
if err.code() == net.err_timed_out_code {
// just skip network timeouts, they are normal
// Skip network timeouts, they are normal
continue
}
eprintln('accept() failed, reason: ${err}; skipping')
Expand Down
2 changes: 1 addition & 1 deletion vlib/net/http/server_test.v
Expand Up @@ -226,7 +226,7 @@ fn test_my_counting_handler_on_random_port() {
}
mut server := &http.Server{
show_startup_message: false
port: 0
addr: ''
accept_timeout: atimeout
handler: MyCountingHandler{}
on_running: fn (mut server http.Server) {
Expand Down

0 comments on commit be0986d

Please sign in to comment.