Skip to content

Commit dadd0c8

Browse files
committed
Improve thread safety
1 parent 27e4c8d commit dadd0c8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

socketio/socketio.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ type ws interface {
125125
}
126126

127127
type Websocket struct {
128-
mu sync.RWMutex
128+
once sync.Once
129+
mu sync.RWMutex
129130
// The Fiber.Websocket connection
130131
Conn *websocket.Conn
131132
// Define if the connection is alive or not
@@ -564,13 +565,14 @@ func (kws *Websocket) read(ctx context.Context) {
564565

565566
// When the connection closes, disconnected method
566567
func (kws *Websocket) disconnected(err error) {
568+
kws.fireEvent(EventDisconnect, nil, err)
569+
567570
// may be called multiple times from different go routines
568571
if kws.IsAlive() {
569-
kws.setAlive(false)
570-
if !kws.IsAlive() {
572+
kws.once.Do(func() {
573+
kws.setAlive(false)
571574
close(kws.done)
572-
}
573-
kws.fireEvent(EventDisconnect, nil, err)
575+
})
574576
}
575577

576578
// Fire error event if the connection is

0 commit comments

Comments
 (0)