Skip to content

Commit

Permalink
problem: connection can be nil when writing and we're panic'ing.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jun 21, 2024
1 parent 3862333 commit a35cdff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header, t
}, nil
}

func (c *Connection) WriteMessage(data []byte) error {
func (c *Connection) WriteMessage(ctx context.Context, data []byte) error {
select {
case <-ctx.Done():
return errors.New("context canceled")
default:
}

if c.msgStateW.IsCompressed() && c.enableCompression {
c.flateWriter.Reset(c.writer)
if _, err := io.Copy(c.flateWriter, bytes.NewReader(data)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
}
case writeRequest := <-r.writeQueue:
// all write requests will go through this to prevent races
if err := r.Connection.WriteMessage(writeRequest.msg); err != nil {
if err := r.Connection.WriteMessage(r.connectionContext, writeRequest.msg); err != nil {
writeRequest.answer <- err
}
close(writeRequest.answer)
Expand Down

0 comments on commit a35cdff

Please sign in to comment.