From 78dd6367e901068d403066fd72abad8192cb2a1a Mon Sep 17 00:00:00 2001 From: Luoyy <10894778+zishang520@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:55:05 +0800 Subject: [PATCH] Fix nil pointer dereference in socket server close method - Added nil check for s.engine before calling Close() to prevent runtime panic - Resolved issue where a nil pointer dereference occurred when closing the socket server - This fixes the error: "runtime error: invalid memory address or nil pointer dereference" --- socket/server.go | 4 +++- socket/socket.go | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/socket/server.go b/socket/server.go index 4af2579..da2db52 100644 --- a/socket/server.go +++ b/socket/server.go @@ -602,7 +602,9 @@ func (s *Server) Close(fn func(error)) { if s.httpServer != nil { s.httpServer.Close(fn) } else { - s.engine.Close() + if s.engine != nil { + s.engine.Close() + } if fn != nil { fn(nil) } diff --git a/socket/socket.go b/socket/socket.go index 8f46330..b3f1ce2 100644 --- a/socket/socket.go +++ b/socket/socket.go @@ -661,7 +661,7 @@ func (s *Socket) _error(err any) { // socket.Disconnect(true) // }) // -// Param: status - if `true`, closes the underlying connection +// Param: status - if `true`, closes the underlying connection func (s *Socket) Disconnect(status bool) *Socket { if !s.Connected() { return s @@ -712,7 +712,7 @@ func (s *Socket) Volatile() *Socket { // socket.Broadcast().Emit("foo", "bar") // }) // -// Return: a new [BroadcastOperator] instance for chaining +// Return: a new [BroadcastOperator] instance for chaining func (s *Socket) Broadcast() *BroadcastOperator { return s.newBroadcastOperator() } @@ -725,7 +725,7 @@ func (s *Socket) Broadcast() *BroadcastOperator { // socket.Local().Emit("foo", "bar") // }) // -// Return: a new [BroadcastOperator] instance for chaining +// Return: a new [BroadcastOperator] instance for chaining func (s *Socket) Local() *BroadcastOperator { return s.newBroadcastOperator().Local() }