Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit af0fd9d

Browse files
committed
examples/chat: Fix race condition
Tricky tricky.
1 parent ff3ea39 commit af0fd9d

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

‎internal/examples/chat/chat.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"log"
8+
"net"
89
"net/http"
910
"sync"
1011
"time"
@@ -69,14 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6970
// subscribeHandler accepts the WebSocket connection and then subscribes
7071
// it to all future messages.
7172
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
72-
c, err := websocket.Accept(w, r, nil)
73-
if err != nil {
74-
cs.logf("%v", err)
75-
return
76-
}
77-
defer c.CloseNow()
78-
79-
err = cs.subscribe(r.Context(), c)
73+
err := cs.subscribe(r.Context(), w, r)
8074
if errors.Is(err, context.Canceled) {
8175
return
8276
}
@@ -117,18 +111,39 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
117111
//
118112
// It uses CloseRead to keep reading from the connection to process control
119113
// messages and cancel the context if the connection drops.
120-
func (cs *chatServer) subscribe(ctx context.Context, c *websocket.Conn) error {
121-
ctx = c.CloseRead(ctx)
122-
114+
func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
115+
var mu sync.Mutex
116+
var c *websocket.Conn
117+
var closed bool
123118
s := &subscriber{
124119
msgs: make(chan []byte, cs.subscriberMessageBuffer),
125120
closeSlow: func() {
126-
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
121+
mu.Lock()
122+
defer mu.Unlock()
123+
closed = true
124+
if c != nil {
125+
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
126+
}
127127
},
128128
}
129129
cs.addSubscriber(s)
130130
defer cs.deleteSubscriber(s)
131131

132+
c2, err := websocket.Accept(w, r, nil)
133+
if err != nil {
134+
return err
135+
}
136+
mu.Lock()
137+
if closed {
138+
mu.Unlock()
139+
return net.ErrClosed
140+
}
141+
c = c2
142+
mu.Unlock()
143+
defer c.CloseNow()
144+
145+
ctx = c.CloseRead(ctx)
146+
132147
for {
133148
select {
134149
case msg := <-s.msgs:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /