From c03d75b57f18cca4ede27add0018b281cc3b4425 Mon Sep 17 00:00:00 2001 From: PotatoCloud <60210021+PotatoCloud@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:55:58 +0800 Subject: [PATCH] fixed OnClose handler panic error --- server.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 51fd3bb..a62f929 100644 --- a/server.go +++ b/server.go @@ -125,7 +125,11 @@ func (s *Server) OnBoot(eng gnet.Engine) gnet.Action { return gnet.None } -func (s *Server) OnShutdown(_ gnet.Engine) {} +func (s *Server) OnShutdown(e gnet.Engine) { + if err := e.Stop(s.ctx); err != nil { + s.logger.Errorf("gnet.OnShutdown error: %s", err) + } +} func (s *Server) OnOpen(c gnet.Conn) ([]byte, gnet.Action) { s.connNum.Add(1) @@ -135,6 +139,9 @@ func (s *Server) OnOpen(c gnet.Conn) ([]byte, gnet.Action) { } func (s *Server) OnClose(c gnet.Conn, _ error) gnet.Action { + if c == nil { + return gnet.None + } s.connNum.Add(-1) // conn closed, remove conn in monitor list s.keepConnTable.Delete(c.RemoteAddr().String())