Skip to content

Commit b1784e6

Browse files
FrauElsterpaveloom
authored andcommitted
adds a lock on writeClose. closes coder#448
1 parent cef8e11 commit b1784e6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

close.go

+7
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ func (c *Conn) closeHandshake(code StatusCode, reason string) error {
169169
}
170170

171171
func (c *Conn) writeClose(code StatusCode, reason string) error {
172+
c.closeMu.Lock()
173+
defer c.closeMu.Unlock()
174+
if c.closeWritten {
175+
return nil
176+
}
177+
172178
ce := CloseError{
173179
Code: code,
174180
Reason: reason,
@@ -193,6 +199,7 @@ func (c *Conn) writeClose(code StatusCode, reason string) error {
193199
if err != nil && !errors.Is(err, net.ErrClosed) {
194200
return err
195201
}
202+
c.closeWritten = true
196203
return nil
197204
}
198205

conn.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ type Conn struct {
7373
closeReadCtx context.Context
7474
closeReadDone chan struct{}
7575

76-
closed chan struct{}
77-
closeMu sync.Mutex
78-
closing bool
76+
closed chan struct{}
77+
closeMu sync.Mutex
78+
closing bool
79+
closeWritten bool
7980

8081
pingCounter atomic.Int64
8182
activePingsMu sync.Mutex

0 commit comments

Comments
 (0)