8000 fix: avoid writing messages after close and improve handshake by FrauElster · Pull Request #476 · coder/websocket · GitHub
[go: up one dir, main page]

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
Prev Previous commit
comment prepareRead and readMu unlocking
  • Loading branch information
mafredri committed Dec 2, 2024
commit 930432faaae0475c7f1a9f2a410f6070a28fe8e7
9 changes: 9 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ func (c *Conn) readLoop(ctx context.Context) (header, error) {
}
}

// prepareRead sets the readTimeout context and returns a done function
// to be called after the read is done. It also returns an error if the
// connection is closed. The reference to the error is used to assign
// an error depending on if the connection closed or the context timed
// out during use. Typically the referenced error is a named return
// variable of the function calling this method.
func (c *Conn) prepareRead(ctx context.Context, err *error) (func(), error) {
select {
case <-c.closed:
Expand Down Expand Up @@ -335,6 +341,9 @@ func (c *Conn) handleControl(ctx context.Context, h header) (err error) {
closeSent := c.closeSentErr != nil
c.closeStateMu.Unlock()

// Only unlock readMu if this connection is being closed becaue
// c.close will try to acquire the readMu lock. We unlock for
// writeClose as well because it may also call c.close.
if !closeSent {
c.readMu.unlock()
_ = c.writeClose(ce.Code, ce.Reason)
Expand Down
0