Skip to content

Commit

Permalink
Fix nil pointer dereference in socket server close method
Browse files Browse the repository at this point in the history
- 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"
  • Loading branch information
zishang520 committed Sep 14, 2024
1 parent a8d73f2 commit 78dd636
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion socket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions socket/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 78dd636

Please sign in to comment.