Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid writing messages after close and improve handshake #476

Merged
merged 12 commits into from
Dec 4, 2024
1 change: 1 addition & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Conn struct {
writeBuf []byte
writeHeaderBuf [8]byte
writeHeader header
closeSent bool
FrauElster marked this conversation as resolved.
Show resolved Hide resolved

closeReadMu sync.Mutex
closeReadCtx context.Context
Expand Down
13 changes: 13 additions & 0 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
}
defer c.writeFrameMu.unlock()

if c.closeSent {
select {
case <-c.closed:
return 0, net.ErrClosed
default:
}
return 0, errors.New("close sent")
}
FrauElster marked this conversation as resolved.
Show resolved Hide resolved

select {
case <-c.closed:
return 0, net.ErrClosed
Expand Down Expand Up @@ -303,6 +312,10 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
}
}

if opcode == opClose {
c.closeSent = true
}

select {
case <-c.closed:
if opcode == opClose {
Expand Down
Loading