added some code to exit the whole process upon panic in http goroutines

This commit is contained in:
hyung-hwan 2024-12-08 00:57:58 +09:00
parent c81e05b4a3
commit 1496951c36
5 changed files with 107 additions and 4 deletions

View File

@ -99,6 +99,11 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
var err error var err error
var je *json.Encoder var je *json.Encoder
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -207,6 +212,11 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
var je *json.Encoder var je *json.Encoder
var cts *ClientConn var cts *ClientConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -286,6 +296,11 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r
var je *json.Encoder var je *json.Encoder
var cts *ClientConn var cts *ClientConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -378,6 +393,11 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter
var je *json.Encoder var je *json.Encoder
var cts *ClientConn var cts *ClientConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -451,6 +471,11 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers) ServeHTTP(w http.Response
var je *json.Encoder var je *json.Encoder
var r *ClientRoute var r *ClientRoute
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -529,6 +554,11 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo
var je *json.Encoder var je *json.Encoder
var p *ClientPeerConn var p *ClientPeerConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) }
}()
c = ctl.c c = ctl.c
je = json.NewEncoder(w) je = json.NewEncoder(w)

View File

@ -327,8 +327,8 @@ func main() {
os.Exit(0) os.Exit(0)
wrong_usage: wrong_usage:
fmt.Fprintf(os.Stderr, "USAGE: %s server --rpc-on=addr:port --ctl-on=addr:port\n", os.Args[0]) fmt.Fprintf(os.Stderr, "USAGE: %s server --rpc-on=addr:port --ctl-on=addr:port [--config-file=file]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s client --rpc-server=addr:port --ctl-on=addr:port [peer-addr:peer-port ...]\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s client --rpc-server=addr:port --ctl-on=addr:port [--config-file=file] [peer-addr:peer-port ...]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s version\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s version\n", os.Args[0])
os.Exit(1) os.Exit(1)

10
hodu.go
View File

@ -1,5 +1,8 @@
package hodu package hodu
import "net/http"
import "os"
import "runtime"
import "sync" import "sync"
const HODU_RPC_VERSION uint32 = 0x010000 const HODU_RPC_VERSION uint32 = 0x010000
@ -39,3 +42,10 @@ func tcp_addr_str_class(addr string) string {
return "tcp" return "tcp"
} }
func dump_call_frame_and_exit(log Logger, req *http.Request, err interface{}) {
var buf []byte
buf = make([]byte, 65536); buf = buf[:min(65536, runtime.Stack(buf, false))]
log.Write("", LOG_ERROR, "[%s] %s %s - %v\n%s", req.RemoteAddr, req.Method, req.URL.String(), err, string(buf))
os.Exit(99) // fatal error. treat panic() as a fatal runtime error
}

View File

@ -2,9 +2,9 @@ package hodu
import "encoding/json" import "encoding/json"
import "net/http" import "net/http"
import "runtime"
import "strconv" import "strconv"
type json_out_server_conn struct { type json_out_server_conn struct {
Id uint32 `json:"id"` Id uint32 `json:"id"`
ServerAddr string `json:"server-addr"` ServerAddr string `json:"server-addr"`
@ -20,6 +20,11 @@ type json_out_server_route struct {
ServerPeerProto ROUTE_PROTO `json:"server-peer-proto"` ServerPeerProto ROUTE_PROTO `json:"server-peer-proto"`
} }
type json_out_stats struct {
NumCPUs int `json:"num-cpus"`
NumGoroutines int `json:"num-goroutines"`
}
// ------------------------------------ // ------------------------------------
type server_ctl_server_conns struct { type server_ctl_server_conns struct {
@ -38,6 +43,10 @@ type server_ctl_server_conns_id_routes_id struct {
s *Server s *Server
} }
type server_ctl_server_stats struct {
s *Server
}
// ------------------------------------ // ------------------------------------
func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@ -46,6 +55,16 @@ func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.R
var err error var err error
var je *json.Encoder var je *json.Encoder
// this deferred function is to overcome the recovering implemenation
// from panic done in go's http server. in that implemenation, panic
// is isolated to a single gorountine. however, i want this program
// to exit immediately once a panic condition is caught. (e.g. nil
// pointer dererence)
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.s.log, req, err) }
}()
s = ctl.s s = ctl.s
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -112,6 +131,11 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
var conn_nid uint64 var conn_nid uint64
var cts *ServerConn var cts *ServerConn
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.s.log, req, err) }
}()
s = ctl.s s = ctl.s
je = json.NewEncoder(w) je = json.NewEncoder(w)
@ -180,10 +204,48 @@ oops:
// ------------------------------------ // ------------------------------------
func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// TODO
} }
// ------------------------------------ // ------------------------------------
func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// TODO
}
// ------------------------------------
func (ctl *server_ctl_server_stats) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var s *Server
var status_code int
var err error
var je *json.Encoder
defer func() {
var err interface{} = recover()
if err != nil { dump_call_frame_and_exit(ctl.s.log, req, err) }
}()
s = ctl.s
je = json.NewEncoder(w)
switch req.Method {
case http.MethodGet:
var stats json_out_stats
stats.NumCPUs = runtime.NumCPU()
stats.NumGoroutines = runtime.NumGoroutine()
status_code = http.StatusOK; w.WriteHeader(status_code)
if err = je.Encode(stats); err != nil { goto oops }
default:
status_code = http.StatusBadRequest; w.WriteHeader(status_code)
}
//done:
s.log.Write("", LOG_DEBUG, "[%s] %s %s %d", req.RemoteAddr, req.Method, req.URL.String(), status_code) // TODO: time taken
return
oops:
s.log.Write("", LOG_ERROR, "[%s] %s %s - %s", req.RemoteAddr, req.Method, req.URL.String(), err.Error())
return
} }

View File

@ -881,6 +881,7 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg
s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}", &server_ctl_server_conns_id{s: &s}) s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}", &server_ctl_server_conns_id{s: &s})
s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes", &server_ctl_server_conns_id_routes{s: &s}) s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes", &server_ctl_server_conns_id_routes{s: &s})
s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes/{route_id}", &server_ctl_server_conns_id_routes_id{s: &s}) s.ctl_mux.Handle(s.ctl_prefix + "/server-conns/{conn_id}/routes/{route_id}", &server_ctl_server_conns_id_routes_id{s: &s})
s.ctl_mux.Handle(s.ctl_prefix + "/stats", &server_ctl_server_stats{s: &s})
s.ctl_addr = make([]string, len(ctl_addrs)) s.ctl_addr = make([]string, len(ctl_addrs))
s.ctl = make([]*http.Server, len(ctl_addrs)) s.ctl = make([]*http.Server, len(ctl_addrs))