Skip to content

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

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

Merged
merged 12 commits into from
Dec 4, 2024
Merged
Next Next commit
adds a lock on writeClose. closes #448
  • Loading branch information
FrauElster committed Sep 5, 2024
commit 0daf07fec93be7da18a697ce5fdd7253f0d14c2f
7 changes: 7 additions & 0 deletions close.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func (c *Conn) closeHandshake(code StatusCode, reason string) error {
}

func (c *Conn) writeClose(code StatusCode, reason string) error {
c.closeMu.Lock()
defer c.closeMu.Unlock()
if c.closeWritten {
return nil
}

ce := CloseError{
Code: code,
Reason: reason,
Expand All @@ -193,6 +199,7 @@ func (c *Conn) writeClose(code StatusCode, reason string) error {
if err != nil && !errors.Is(err, net.ErrClosed) {
return err
}
c.closeWritten = true
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ type Conn struct {
closeReadCtx context.Context
closeReadDone chan struct{}

closed chan struct{}
closeMu sync.Mutex
closing bool
closed chan struct{}
closeMu sync.Mutex
closing bool
closeWritten bool

pingCounter atomic.Int64
activePingsMu sync.Mutex
Expand Down
Loading