From 464a550c68c89b523bc629556028623681e0a4df Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 9 Dec 2024 17:23:26 +0900 Subject: [PATCH] added a stop request guard in starting a control channel --- client.go | 32 +++++++++++++++++++++++++------- server.go | 29 +++++++++++++++++++---------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index 87945cd..6d79e0f 100644 --- a/client.go +++ b/client.go @@ -1268,20 +1268,38 @@ func (c *Client) RunCtlTask(wg *sync.WaitGroup) { // by creating the listener explicitly. // err = cs.ListenAndServe() // err = cs.ListenAndServeTLS("", "") // c.tlscfg must provide a certificate and a key - l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) - if err == nil { - if c.ctltlscfg == nil { - err = cs.Serve(l) - } else { - err = cs.ServeTLS(l, "", "") // c.ctltlscfg must provide a certificate and a key + + //cs.shuttingDown(), as the name indicates, is not expoosed by the net/http + //so I have to use my own indicator to check if it's been shutdown.. + // + if c.stop_req.Load() == false { + // this guard has a flaw in that the stop request can be made + // between the check above and net.Listen() below. + l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) + if err == nil { + if c.stop_req.Load() == false { + // check it again to make the guard slightly more stable + // although it's still possible that the stop request is made + // after Listen() + if c.ctltlscfg == nil { + err = cs.Serve(l) + } else { + err = cs.ServeTLS(l, "", "") // c.ctltlscfg must provide a certificate and a key + } + } else { + err = fmt.Errorf("stop requested") + } + l.Close() } - l.Close() + } else { + err = fmt.Errorf("stop requested") } if errors.Is(err, http.ErrServerClosed) { c.log.Write("", LOG_INFO, "Control channel[%d] ended", i) } else { c.log.Write("", LOG_ERROR, "Control channel[%d] error - %s", i, err.Error()) } + l_wg.Done() }(idx, ctl) } diff --git a/server.go b/server.go index b16a63e..6cd39d4 100644 --- a/server.go +++ b/server.go @@ -1020,17 +1020,26 @@ func (s *Server) RunCtlTask(wg *sync.WaitGroup) { s.log.Write("", LOG_INFO, "Control channel[%d] started on %s", i, s.ctl_addr[i]) - // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() - // err = cs.ListenAndServe() - // err = cs.ListenAndServeTLS("", "") - l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) - if err == nil { - if s.ctltlscfg == nil { - err = cs.Serve(l) - } else { - err = cs.ServeTLS(l, "", "") // s.ctltlscfg must provide a certificate and a key + + if s.stop_req.Load() == false { + // defeat hard-coded "tcp" in ListenAndServe() and ListenAndServeTLS() + // err = cs.ListenAndServe() + // err = cs.ListenAndServeTLS("", "") + l, err = net.Listen(tcp_addr_str_class(cs.Addr), cs.Addr) + if err == nil { + if s.stop_req.Load() == false { + if s.ctltlscfg == nil { + err = cs.Serve(l) + } else { + err = cs.ServeTLS(l, "", "") // s.ctltlscfg must provide a certificate and a key + } + } else { + err = fmt.Errorf("stop requested") + } + l.Close() } - l.Close() + } else { + err = fmt.Errorf("stop requested") } if errors.Is(err, http.ErrServerClosed) { s.log.Write("", LOG_INFO, "Control channel[%d] ended", i)