Skip to content

Commit 10b0b40

Browse files
committed
Fix deadlock
1 parent 343c1e7 commit 10b0b40

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

ws/client.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package ws
22

33
import (
44
"fmt"
5+
"github.com/gorilla/websocket"
6+
"github.com/rs/xid"
57
"github.com/rs/zerolog"
8+
"github.com/rs/zerolog/log"
9+
"github.com/screego/server/ws/outgoing"
610
"net"
711
"net/http"
812
"strings"
913
"time"
10-
"github.com/gorilla/websocket"
11-
"github.com/rs/xid"
12-
"github.com/rs/zerolog/log"
13-
"github.com/screego/server/ws/outgoing"
1414
)
1515

1616
var ping = func(conn *websocket.Conn) error {
@@ -78,10 +78,12 @@ func newClient(conn *websocket.Conn, req *http.Request, read chan ClientMessage,
7878
func (c *Client) Close() {
7979
c.once.Do(func() {
8080
c.conn.Close()
81-
c.read <- ClientMessage{
82-
Info: c.info,
83-
Incoming: &Disconnected{},
84-
}
81+
go func() {
82+
c.read <- ClientMessage{
83+
Info: c.info,
84+
Incoming: &Disconnected{},
85+
}
86+
}()
8587
})
8688
}
8789

@@ -125,8 +127,8 @@ func (c *Client) startWriteHandler(pingPeriod time.Duration) {
125127
dead := false
126128
conClosed := func() {
127129
dead = true
128-
pingTicker.Stop()
129130
c.Close()
131+
pingTicker.Stop()
130132
}
131133
defer conClosed()
132134
defer func() {
@@ -135,10 +137,12 @@ func (c *Client) startWriteHandler(pingPeriod time.Duration) {
135137
for {
136138
select {
137139
case reason := <-c.info.Close:
138-
if reason != CloseDone {
140+
if reason == CloseDone {
141+
return
142+
} else {
139143
_ = c.conn.CloseHandler()(websocket.CloseNormalClosure, reason)
144+
conClosed()
140145
}
141-
return
142146
case message := <-c.info.Write:
143147
if dead {
144148
c.debug().Msg("WebSocket write on dead connection")

0 commit comments

Comments
 (0)