Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connection: fix: reader go-routine is leaked on connection close
When a message was sent and it's response was received while the connection was closed or an error happened, the reader go-routine could get stuck and be leaked. The reader go routine tries to send a received message to the unbuffered c.rpc channel via the dispatch0() and dispatchN() methods. The call() method reads from the rpc channel. If an error happened while the dispatch method sends a message to the rpc channel, the call() method could terminate because it read an error from c.errors or because c.errors was closed. To prevent the scenario: - the reader go-routine now closes c.rpc when it terminates, - The call() method, reads from c.rpc until a message was received or it is closed. When c.rpc is closed, it reads an error from c.errors or wait until c.errors is closed. When it reads an error, it returns it. If it is closed it returns ErrClosed. This ensures that the messages is read from c.rpc before call() returns. It also ensures that when a message was received that it is processed. Previously it could happen that the message was silently ignored because c.errors returned an error or was closed.
- Loading branch information