From 1496951c3635c9ddd9feb5250afdfbdcc7da1dc0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 8 Dec 2024 00:57:58 +0900 Subject: [PATCH] added some code to exit the whole process upon panic in http goroutines --- client-ctl.go | 30 +++++++++++++++++++++++ cmd/main.go | 4 ++-- hodu.go | 10 ++++++++ server-ctl.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-- server.go | 1 + 5 files changed, 107 insertions(+), 4 deletions(-) diff --git a/client-ctl.go b/client-ctl.go index 2fcafa3..bf75697 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -99,6 +99,11 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R var err error 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 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 cts *ClientConn + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + c = ctl.c 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 cts *ClientConn + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + c = ctl.c 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 cts *ClientConn + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + c = ctl.c 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 r *ClientRoute + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + c = ctl.c 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 p *ClientPeerConn + defer func() { + var err interface{} = recover() + if err != nil { dump_call_frame_and_exit(ctl.c.log, req, err) } + }() + c = ctl.c je = json.NewEncoder(w) diff --git a/cmd/main.go b/cmd/main.go index 68bcebf..93a2c45 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -327,8 +327,8 @@ func main() { os.Exit(0) 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, " %s client --rpc-server=addr:port --ctl-on=addr:port [peer-addr:peer-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 [--config-file=file] [peer-addr:peer-port ...]\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s version\n", os.Args[0]) os.Exit(1) diff --git a/hodu.go b/hodu.go index aa7faf5..3e5235b 100644 --- a/hodu.go +++ b/hodu.go @@ -1,5 +1,8 @@ package hodu +import "net/http" +import "os" +import "runtime" import "sync" const HODU_RPC_VERSION uint32 = 0x010000 @@ -39,3 +42,10 @@ func tcp_addr_str_class(addr string) string { 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 +} diff --git a/server-ctl.go b/server-ctl.go index b6f0845..ad58262 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -2,9 +2,9 @@ package hodu import "encoding/json" import "net/http" +import "runtime" import "strconv" - type json_out_server_conn struct { Id uint32 `json:"id"` ServerAddr string `json:"server-addr"` @@ -20,6 +20,11 @@ type json_out_server_route struct { 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 { @@ -38,6 +43,10 @@ type server_ctl_server_conns_id_routes_id struct { s *Server } +type server_ctl_server_stats struct { + s *Server +} + // ------------------------------------ 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 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 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 cts *ServerConn + 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) @@ -180,10 +204,48 @@ oops: // ------------------------------------ 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) { + // 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 } diff --git a/server.go b/server.go index 823ca7e..f0b2862 100644 --- a/server.go +++ b/server.go @@ -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}/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 + "/stats", &server_ctl_server_stats{s: &s}) s.ctl_addr = make([]string, len(ctl_addrs)) s.ctl = make([]*http.Server, len(ctl_addrs))