Skip to content

Commit 919a893

Browse files
committed
fix: connection reader block
1 parent 1e38f27 commit 919a893

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

connection.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,12 @@ func (c *Connection) dispatch0(f frame) {
640640
// kthx - all reads reset our deadline. so we can drop this
641641
default:
642642
// lolwat - channel0 only responds to methods and heartbeats
643-
if err := c.closeWith(ErrUnexpectedFrame); err != nil {
644-
Logger.Printf("error sending connectionCloseOk with ErrUnexpectedFrame, error: %+v", err)
645-
}
643+
// closeWith use call don't block reader
644+
go func() {
645+
if err := c.closeWith(ErrUnexpectedFrame); err != nil {
646+
Logger.Printf("error sending connectionCloseOk with ErrUnexpectedFrame, error: %+v", err)
647+
}
648+
}()
646649
}
647650
}
648651

@@ -689,9 +692,12 @@ func (c *Connection) dispatchClosed(f frame) {
689692
// we are already closed, so do nothing
690693
default:
691694
// unexpected method on closed channel
692-
if err := c.closeWith(ErrClosed); err != nil {
693-
Logger.Printf("error sending connectionCloseOk with ErrClosed, error: %+v", err)
694-
}
695+
// closeWith use call don't block reader
696+
go func() {
697+
if err := c.closeWith(ErrClosed); err != nil {
698+
Logger.Printf("error sending connectionCloseOk with ErrClosed, error: %+v", err)
699+
}
700+
}()
695701
}
696702
}
697703
}
@@ -866,13 +872,14 @@ func (c *Connection) call(req message, res ...message) error {
866872
}
867873
}
868874

869-
msg, ok := <-c.rpc
870-
if !ok {
871-
err, errorsChanIsOpen := <-c.errors
872-
if !errorsChanIsOpen {
873-
return ErrClosed
875+
var msg message
876+
select {
877+
case e, ok := <-c.errors:
878+
if ok {
879+
return e
874880
}
875-
return err
881+
return ErrClosed
882+
case msg = <-c.rpc:
876883
}
877884

878885
// Try to match one of the result types

0 commit comments

Comments
 (0)