Skip to content

Commit

Permalink
Fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Oct 9, 2020
1 parent 343c1e7 commit 10b0b40
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package ws

import (
"fmt"
"github.com/gorilla/websocket"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/screego/server/ws/outgoing"
"net"
"net/http"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/rs/xid"
"github.com/rs/zerolog/log"
"github.com/screego/server/ws/outgoing"
)

var ping = func(conn *websocket.Conn) error {
Expand Down Expand Up @@ -78,10 +78,12 @@ func newClient(conn *websocket.Conn, req *http.Request, read chan ClientMessage,
func (c *Client) Close() {
c.once.Do(func() {
c.conn.Close()
c.read <- ClientMessage{
Info: c.info,
Incoming: &Disconnected{},
}
go func() {
c.read <- ClientMessage{
Info: c.info,
Incoming: &Disconnected{},
}
}()
})
}

Expand Down Expand Up @@ -125,8 +127,8 @@ func (c *Client) startWriteHandler(pingPeriod time.Duration) {
dead := false
conClosed := func() {
dead = true
pingTicker.Stop()
c.Close()
pingTicker.Stop()
}
defer conClosed()
defer func() {
Expand All @@ -135,10 +137,12 @@ func (c *Client) startWriteHandler(pingPeriod time.Duration) {
for {
select {
case reason := <-c.info.Close:
if reason != CloseDone {
if reason == CloseDone {
return
} else {
_ = c.conn.CloseHandler()(websocket.CloseNormalClosure, reason)
conClosed()
}
return
case message := <-c.info.Write:
if dead {
c.debug().Msg("WebSocket write on dead connection")
Expand Down

0 comments on commit 10b0b40

Please sign in to comment.